简体   繁体   中英

Not able to connect to SQL Server using configuration manager

I am trying to connect to a SQL Server database using the configuration manager.

The config file code:

<connectionStrings>
    <add name="DBCS"
         connectionString="data source =.\\SQLEXPRESS; database = Sample2; integrated security = SSPI;"
         providerName ="System.Data.SqlClient" />
</connectionStrings>

The C# code below:

string SC = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

//string SC = ("data source =.\\SQLEXPRESS; database = Sample2; Integrated Security = SSPI;");
SqlConnection con = new SqlConnection(SC);

SqlCommand cmd = new SqlCommand("Select * from tblemployee", con);
con.Open();

GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();

When I try to run the application I get an error at

con.Open();

Error:

An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

Additional information: Instance failure.

Please help.

You need only one \\ before SQLExpress in your connection string:

<add name="DBCS"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=.\SQLEXPRESS; database = Sample2;Integrated Security=SSPI"/>

Check the documentation for further details

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