简体   繁体   中英

Sql Connection Problem

I have a sqlconnection problem. i have perfectly connection to sql server when im using sqldatasource. but when im try use SqlConnection object throw an exception.

  string qstring = "Data Source=****;Initial Catalog=**;User ID=**;Password=**";
  SqlConnection con = new SqlConnection(qstring);
  con.Open(); (exception thrown here)

exception is : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Sql server is in different machine and i can connect using SqlDataSource object for example i can bind a grid by that way. But i must connect with SqlConnection object

Connection string is true because i have it from SqlDataSource...

Thx for your helps..

You might also want to make sure that the SQL Server is set up to accept remote connections. (for sql2005 Configuration tools -> Surface AreaConfiguration-> Services and Connections -> database, Local and remote connections )

If the code that you have shown is what you have then I'm not surprised that you couldn't connect.

You'll need to have the correct Data Source, UserID and Password in qstring. As it stands asterisk characters won't mean anything.

A new point A further point, which is often forgotten, SQLConnection will only work with Microsoft SQL Server. If your SQL is provided by a different manufacturer the use OleDBConnection instead.

Is your SQL server is in the same machine ? if yes , Check the SQL server service is running , If its in a different machine,check your development machine has connectivity to it .You can ping to that machine to verify this. You can also check the surface area connection wizard to check whether the SQL server support remote connections , as snomag said

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