I have code for changing connection string in app.config. But when i change the database i encounter an error because the linq to sql.dbml is not updated to the database i changed. I need to close the program and open again to take effect the changes. What should i do to update my linq to 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);