如何在数据库连接动态更改时将LINQ更新为SQL.dbml

本文关键字:LINQ 更新 dbml SQL 数据库连接 动态 | 更新日期: 2023-09-27 18:24:34

我在app.config中有更改连接字符串的代码。但当我更改数据库时,我遇到了一个错误,因为sql.dbml的linq没有更新到我更改的数据库我需要关闭程序并再次打开以使更改生效。我应该怎么做才能将linq更新为sql.dbml?

var name = "DbName";
   bool isNew = false;
            string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", name));
            XmlNode node;
            isNew = list.Count == 0;
            if (isNew)
            {
                node = doc.CreateNode(XmlNodeType.Element, "add", null);
                XmlAttribute attribute = doc.CreateAttribute("name");
                attribute.Value = name;
                node.Attributes.Append(attribute);
                attribute = doc.CreateAttribute("connectionString");
                attribute.Value = "";
                node.Attributes.Append(attribute);
                attribute = doc.CreateAttribute("providerName");
                attribute.Value = "System.Data.SqlClient";
                node.Attributes.Append(attribute);
            }
            else
            {
                node = list[0];
            }
            string conString = node.Attributes["connectionString"].Value;
            SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder(conString);
            conStringBuilder.DataSource = txtConnectServername.Text;
            conStringBuilder.InitialCatalog = "AlTayerDB";
            conStringBuilder.PersistSecurityInfo = true;
            conStringBuilder.UserID = txtConnectUserId.Text;
            conStringBuilder.Password = txtConnectAdapterPassword.Text;
            conStringBuilder.MultipleActiveResultSets = true;
            node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
            if (isNew)
            {
                doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
            }
            doc.Save(path);

如何在数据库连接动态更改时将LINQ更新为SQL.dbml

我建议您遵循以下内容-如何在运行时更新/插入/删除配置文件?有关此主题的完整信息:

在运行时修改配置文件中的现有值。

由于Configuration.AppSettings属性是只读的,为了修改当前应用程序设置值,我们必须使用XmlDocument类将应用程序配置文件直接更新为XML文档。

这是原始的App.config文件:

<configuration>
  <appSettings>
    <add key="Setting1" value="1" />
    <add key="Setting2" value="2" />
  </appSettings>
</configuration>

以下是修改应用程序设置值的代码示例:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement element in xmlDoc.DocumentElement)
{
    if (element.Name.Equals("appSettings"))
    {
        foreach (XmlNode node in element.ChildNodes)
        {
            if (node.Attributes[0].Value.Equals("Setting1"))
            {
                node.Attributes[1].Value = "New Value";
            }
        }
    }
}
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("appSettings");

如果这不符合要求,请检查以下参考资料:
动态更改app.config文件中的值
应用程序配置更改值
使用ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)以编程方式更新app.config文件;