在我的app.config文件中,我有一个名为DBEntities1的默认连接字符串,它对数据库具有只读访问权限(由于用户ID: appUser ): / p>
<connectionStrings>
<add name="DBEntities1" connectionString="metadata=res://*/DBModel1.csdl|res://*/DBModel1.ssdl|res://*/DBModel1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=appUser;password=pass;database=db"" providerName="System.Data.EntityClient" />
</connectionStrings>
但是在我的程序中,我试图通过修改并重新使用相同的连接字符串来更改用户ID和密码,以在数据库上获得完全访问权限,如下所示:
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DBEntities1"];
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(settings.ToString());
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(entityBuilder.ProviderConnectionString);
sqlBuilder.UserID = "sosso";
sqlBuilder.Password = "password";
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
String newConnectioStrings = entityBuilder.ToString();
using(EntityConnection connection = new EntityConnection(newConnectionStrings))
{
connection.Open();
DBEntities1 context = new DBEntities1();
client c = new client();
c.table_client = 1;
c.idClient = 0;
context.client.Add(c);
context.SaveChanges();
connection.Close();
}
,但是程序似乎仍在使用具有旧用户ID的旧connectionStrings,并在上下文中返回错误。方法说:
MySqlException:禁止用户'INSERT'命令:'client'表上的'appUser' @'@ localhost'。
请,我如何使程序使用新的connectionStrings ??????