C# 如何先更改与数据库的连接字符串
我想更改数据库第一个项目中的数据源地址 这是我的代码:C# 如何先更改与数据库的连接字符串,c#,entity-framework,connection-string,npgsql,C#,Entity Framework,Connection String,Npgsql,我想更改数据库第一个项目中的数据源地址 这是我的代码: string connect = "metadata=res://*/ornekDBEnt.csdl|res://*/ornekDBEnt.ssdl|res://*/ornekEnt.msl;provider=Npgsql;provider connection string="Host=127.0.0.1;Username=postgres;Password=pass1;Persist Security Info=Tru
string connect = "metadata=res://*/ornekDBEnt.csdl|res://*/ornekDBEnt.ssdl|res://*/ornekEnt.msl;provider=Npgsql;provider connection string="Host=127.0.0.1;Username=postgres;Password=pass1;Persist Security Info=True;Database=ornekdb"";
updateConfigFile(connect);
public void updateConfigFile(string con)
{
//updating config file
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
xElement.FirstChild.Attributes[1].Value = con;
}
}
//writing the connection string in config file
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
我得到以下错误
System.ArgumentException:'不支持关键字:'主机''
请不要添加不相关的标签。还请包括您收到的错误。System.ArgumentException:'关键字不受支持:'主机''。这是连接字符串的格式:Provider=PostgreSQL OLE DB Provider;数据源=myserver地址;位置=我的数据库;用户ID=我的用户名;密码=我的密码@Bibin无法使用您的codeSystem.ArgumentException:'关键字不受支持:'数据源''