简体   繁体   English

如何在web.config文件中写入连接字符串

[英]how to write connectionstring in web.config file

My code works well but after trying to host it. 我的代码运行良好,但是尝试托管之后。 Its database always response with the null value . 其数据库始终以null值响应。 I failed to host it. 我无法托管。 and now when i try to debug in my PC its also have the same problem of null response. 现在,当我尝试在PC上进行调试时,它也会遇到null响应相同的问题。

my class file and its scalar query code. 我的类文件及其标量查询代码。

 public Object ExecuteScalarQuery(String sp)
        {
            String _ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["rfid"].ConnectionString;

           // string _ConnString = ConfigurationManager.ConnectionStrings["rfid"].ConnectionString;

            SqlConnection myConnection = new SqlConnection(_ConnString);
            SqlCommand cmd = new SqlCommand(sp, myConnection);
            Object result = 0;

            try
            {
                myConnection.Open();
                result = cmd.ExecuteScalar();

            }
            catch (Exception ex)
            {
                //if (myConnection.State == ConnectionState.Open)
                //    myConnection.Close();

            }
            finally
            {
                //if (myConnection.State == ConnectionState.Open)
                //    myConnection.Close();
            }
            return result;
        }

And web.config file having connectionstring 和具有连接字符串的web.config文件

    <connectionStrings>
<add name="rfid" connectionString="Data Source=CHINTAN-PC\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True " providerName="System.Data.SqlClient"/>

while doing step by step debugging its connectionstring look like this which is not being working. 在逐步调试其连接字符串时看起来像这样,这是行不通的。

"Data Source=CHINTAN-PC\\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True "

One thing to check is that results are being returned by your stored procedure. 要检查的一件事是存储过程正在返回结果。 I copied your code, made a table and a stored procedure to query all records from it, and it returned null when the table was empty and the value of the first column of the first row when I added a couple records. 我复制了您的代码,创建了一个表和一个存储过程以从中查询所有记录,当表为空时,它返回null,当我添加几个记录时,它返回第一行第一列的值。

添加属性“池”。

<add name="rfid" connectionString="Data Source=CHINTAN-PC\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True; pooling=false;" providerName="System.Data.SqlClient"/>

go to server explorer, select you data base. 转到服务器资源管理器,选择您的数据库。 in the right pane(Properties) copy connection string and paste it. 在右窗格(属性)中复制连接字符串并将其粘贴。 if that doesn't work. 如果那不起作用。 go to sql management studio. 去SQL管理工作室。 its your windows authentication and sql authentication problem. 它是您的Windows身份验证和sql身份验证问题。 make a new sql authentication login and give userid="" and password="" like "sa" and "sa123" in the connection string. 进行新的sql身份验证登录,并在连接字符串中输入userid =“”和password =“”,例如“ sa”和“ sa123”。

使用toString检索字符串值

String _ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["rfid"].ConnectionString.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM