C# 动态更改连接字符串
我有一个场景,其中我将获得动态生成的数据库。我想为每个数据库创建一个连接字符串。因此,只有初始目录参数将更改。我将从web配置文件获取其他参数。它已经具有连接字符串的所有条目。我只想根据创建的数据库动态更改初始目录零件C# 动态更改连接字符串,c#,asp.net,sql-server-2008,connection-string,C#,Asp.net,Sql Server 2008,Connection String,我有一个场景,其中我将获得动态生成的数据库。我想为每个数据库创建一个连接字符串。因此,只有初始目录参数将更改。我将从web配置文件获取其他参数。它已经具有连接字符串的所有条目。我只想根据创建的数据库动态更改初始目录零件 请建议是否有任何方法可以轻松做到这一点。使用。从配置文件进行设置,并更改每个数据库的设置。使用。从配置文件进行设置,并更改每个数据库的设置。使用。从配置文件进行设置,并更改每个数据库的设置。使用。从配置文件进行设置,并更改每个数据库的属性。使用SqlConnectionStrin
请建议是否有任何方法可以轻松做到这一点。使用。从配置文件进行设置,并更改每个数据库的设置。使用。从配置文件进行设置,并更改每个数据库的设置。使用。从配置文件进行设置,并更改每个数据库的设置。使用。从配置文件进行设置,并更改每个数据库的属性。使用SqlConnectionStringBuilder类
SqlConnectionStringBuilder conn = new SqlConnectionStringBuilder(ConnectionStringFromConfig)
{ InitialCatalog = "your CatalogName" }; // you can add other parameters.
然后在需要时使用此连接字符串
conn.ConnectionString;
使用SqlConnectionStringBuilder类
SqlConnectionStringBuilder conn = new SqlConnectionStringBuilder(ConnectionStringFromConfig)
{ InitialCatalog = "your CatalogName" }; // you can add other parameters.
然后在需要时使用此连接字符串
conn.ConnectionString;
使用SqlConnectionStringBuilder类
SqlConnectionStringBuilder conn = new SqlConnectionStringBuilder(ConnectionStringFromConfig)
{ InitialCatalog = "your CatalogName" }; // you can add other parameters.
然后在需要时使用此连接字符串
conn.ConnectionString;
使用SqlConnectionStringBuilder类
SqlConnectionStringBuilder conn = new SqlConnectionStringBuilder(ConnectionStringFromConfig)
{ InitialCatalog = "your CatalogName" }; // you can add other parameters.
然后在需要时使用此连接字符串
conn.ConnectionString;
尝试使用这行代码,它将根据需要更改web配置文件中的连接字符串。我是用TextBox做的
bool isNew = false;
string path = Server.MapPath("~/Web.Config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", "conn"));
XmlNode node;
isNew = list.Count == 0;
if (isNew)
{
node = doc.CreateNode(XmlNodeType.Element, "add", null);
XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = "conn";
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.InitialCatalog = T1.Text;
node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
if (isNew)
{
doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
}
doc.Save(path);
这可能是你的障碍的一个暗示 尝试使用这行代码,它将根据需要更改web配置文件中的连接字符串。我是用TextBox做的
bool isNew = false;
string path = Server.MapPath("~/Web.Config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", "conn"));
XmlNode node;
isNew = list.Count == 0;
if (isNew)
{
node = doc.CreateNode(XmlNodeType.Element, "add", null);
XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = "conn";
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.InitialCatalog = T1.Text;
node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
if (isNew)
{
doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
}
doc.Save(path);
这可能是你的障碍的一个暗示 尝试使用这行代码,它将根据需要更改web配置文件中的连接字符串。我是用TextBox做的
bool isNew = false;
string path = Server.MapPath("~/Web.Config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", "conn"));
XmlNode node;
isNew = list.Count == 0;
if (isNew)
{
node = doc.CreateNode(XmlNodeType.Element, "add", null);
XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = "conn";
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.InitialCatalog = T1.Text;
node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
if (isNew)
{
doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
}
doc.Save(path);
这可能是你的障碍的一个暗示 尝试使用这行代码,它将根据需要更改web配置文件中的连接字符串。我是用TextBox做的
bool isNew = false;
string path = Server.MapPath("~/Web.Config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", "conn"));
XmlNode node;
isNew = list.Count == 0;
if (isNew)
{
node = doc.CreateNode(XmlNodeType.Element, "add", null);
XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = "conn";
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.InitialCatalog = T1.Text;
node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
if (isNew)
{
doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
}
doc.Save(path);
这可能是你的障碍的一个暗示