使用web.config的SQL Server连接问题. [英] Problem with SQL Server connection using web.config.

查看:109
本文介绍了使用web.config的SQL Server连接问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在ASP.Net应用程序中使用Webform,在web.config中使用连接字符串会遇到问题.连接字符串如下:

web.config中的代码:

We are getting problem with using connection string in web.config for a webform in ASP.Net application. The connection string is as follows:

Code in web.config:

<configuration>
     <appSettings>
            <add key="connectionString" value="Server=.\\SQLEXPRESS; database=MLM; Trusted_Connection=True"/>
      </appSettings>
        ....


WebForm中的代码如下:


And Code in WebForm is as below:

SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter adp;
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
          string connecStr = System.Configuration.ConfigurationSettings.AppSettings["connectionString"]; 
            con = new SqlConnection(connecStr); 
            cmd = new SqlCommand("Select * from tblNodeTree");
            cmd.Connection = con;
            ds = new DataSet(); 
            adp = new SqlDataAdapter(cmd);
            adp.Fill(ds, "tblNodeTree");	//  This line shows error message "instance failure" 
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }



错误消息是:实例故障
但是在替换第一行代码时效果很好:



The Error Message is : Instance failure
But it works fine on replacing first line of code:

string connecStr = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
With
   string connecStr= "Server=.\\SQLEXPRESS; database=MLM; Trusted_Connection=True";   


web.config的数据库连接在这里有什么问题.最佳解决方案应该是什么.请注意.


What''s the problem here with web.config''s database connection. And what should be its optimum solution. Kindly advice.

推荐答案

转义序列是问题所在.在那里删除一个\.

连接到SQL Server 2005 Express时,修复实例失败 [
Escape sequence is the issue. Remove one \ there.

Fixing Instance Failure when connecting to SQL Server 2005 Express[^]


您应将连接字符串放在connectionStrings部分中.

此处开始 [此处 [ ^ ].
You should place the connection string in the connectionStrings section.

Start here[^] and here[^].


这篇关于使用web.config的SQL Server连接问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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