简体   繁体   中英

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 . I failed to host it. and now when i try to debug in my PC its also have the same problem of null response.

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

    <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.

添加属性“池”。

<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. its your windows authentication and sql authentication problem. make a new sql authentication login and give userid="" and password="" like "sa" and "sa123" in the connection string.

使用toString检索字符串值

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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