在ASP.NET C中动态更改连接字符串# [英] Change connection string dynamically in ASP.NET C#

查看:53
本文介绍了在ASP.NET C中动态更改连接字符串#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下拉列表更改后更改我的连接字符串。



我尝试了什么:



i没有任何想法。请帮助我

解决方案

如果你想动态更改连接字符串,那么web.config不是可能是保持价值的合适场所。那是因为,这些是只读的人。



1.如果下拉列表中的列表已修复,那么您可以使用不同的名称/密钥在同一个web.config文件中保留多个连接字符串,因此您可以在它们之间切换。

2.如果你想修改连接字符串,那么设置将更适合。

有关设置的详细帮助,请检查以下MSDN链接

在C#中使用设置 [ ^ ]



尝试这些方法和万一你遇到任何问题,请告诉我。



希望,有帮助:)


你无法改变配置中的连接字符串,您必须在配置中拥有所有可能的连接,并选择在代码中使用哪个连接,或者动态创建连接



< pre lang =XML> &l t; connectionStrings >
< add name = MyCon1 connectionString = server =。\ SQL2008;数据库DB1 =; Trusted_Connection = true; / >
< add name = MyCon2 connectionString = server =。\ SQL2008;数据库DB2 =; Trusted_Connection = true; / >
< add name = MyCon3 connectionString = server =。\ SQL2008;数据库= {0}; UID = {1}; pwd = {2}; / >
< / connectionStrings >





 SqlConnection con =  null ; 

if (someCondition)
{
// 使用MyCon1
con = new SqlConnection(ConfigurationManager.ConnectionStrings [ MyCon1]。ConnectionString);
}
else
{
// 使用MyCon2
con = new SqlConnection(ConfigurationManager.ConnectionStrings [ MyCon2]的ConnectionString)。
}

// 或者您可以手动构造字符串。我假设您要使用
// 来自控件的数据字符串,所以如果你有一个数据库的变量
string dbToUse = mydb;
con = new SqlConnection( string .Format( server = .; database = {0};,dbToUse));

// 或者您可以将令牌放在连接字符串中并将其保存在web config
string username = ;
string password = mypassword;

con = new SqlConnection( string .Format(ConfigurationManager.ConnectionStrings [< span class =code-string>
MyCon3]。ConnectionString,dbToUse,username,password));


i want to change my connection string after drop down list change.

What I have tried:

i have no any idea.Please help me

解决方案

If you want to change the connection string dynamically then web.config is not probably the right place to keep the value. That's because, these are readonly peoperties.

1. If you the list in the dropdown is fixed then you can keep multiple connection strings in the same web.config file with dfferent name/key and accordingly you can switch among them.
2. If you want to modify connection string then Settings will suit more.
For detailed help on Settings check following MSDN link
Using Settings in C#[^]

Try these approaches and in case you face any issue, please let me know.

Hope, it helps :)


You can't change the connection string in the config, you either have to have all possible connections in the config and choose which one to use in your code, or create the connection dynamically

<connectionStrings>
  <add name="MyCon1" connectionString="server=.\SQL2008; database=DB1; Trusted_Connection=true;"/>
  <add name="MyCon2" connectionString="server=.\SQL2008; database=DB2; Trusted_Connection=true;"/>
  <add name="MyCon3" connectionString="server=.\SQL2008; database={0}; uid={1}; pwd={2};"/>
</connectionStrings>



SqlConnection con = null;

if (someCondition)
{
    // use MyCon1
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon1"].ConnectionString);
}
else
{
    // use MyCon2
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon2"].ConnectionString);
}
            
// or you can construct the string manually.  I'm going to assume you want to use
// data from your controls in the string, so if you have a variable for the database
string dbToUse = "mydb";
con = new SqlConnection(string.Format("server=.;database={0};", dbToUse));

// or you could put the tokens in the connection string itself and keep it in the web config
string username = "me";
string password = "mypassword";
            
con = new SqlConnection(string.Format(ConfigurationManager.ConnectionStrings["MyCon3"].ConnectionString, dbToUse, username, password));


这篇关于在ASP.NET C中动态更改连接字符串#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆