1、如上所示,我们可以将数据库字符串配置在的节点中。推荐将数据库连接字符串定义在该节点中。
在.cs文件中调用该字符串的方法:
string str=System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection sqlstr = new SqlConnection(str);
//或者可以这样写
string str=System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
SqlConnection sqlstr = new SqlConnection(str);
2、也可以将数据库字符串配置在的节点中,不推荐将数据库连接字符串定义在该节点中。
在.cs文件中调用该字符串的方法:
string str=System.Configuration.ConfigurationManager.appSettings["conn"].ToString();
SqlConnection sqlstr = new SqlConnection(str);
web.config中连接字符串的写法也有很多种:
其中:Data source 等价于Server 后面都是跟要连接的服务器名
如:Data source = (local) 或者 Data source = localhost **注意** (local)==localhost 而没有(localhost)和local这两种写法;
其中:Initial catalog等价于Database 后面都跟要连接的数据库名
如: Initial catalog = Login 或者 Database = Login
当连接字符串中出现Integrated Security=True或者SSPI时,连接语句中的 UserID, Pwd 是不起作用的,即采用windows身份验证模式。