开发者

Can't connect to local SQL Server in C#

开发者 https://www.devze.com 2023-03-17 18:20 出处:网络
What I want to do is an application that uses a SQL Server 2008 database, but I can\'t manage to connect to the db.

What I want to do is an application that uses a SQL Server 2008 database, but I can't manage to connect to the db.

Here is my code:

public Form1()
{
     InitializeComponent(); 
     connection = new SqlConnection("Se开发者_运维问答rver=(local);Integrated Security=true");
     connection.Open();    
     CreateDb();    
     this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
}

The error that I'm getting says:

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)

I am trying to connect using windows authentication... What am I doing wrong?


Check that TCP/IP connections are enabled in the SQL Server Configuration Manager. Also, if you have installed it as an instance, you will need to specify the server name as .\INSTANCE_NAME.


Try this one :

Data Source=localhost\MyInstanceName;Initial Catalog=myDataBase;Integrated Security=SSPI;

Your instance name is probably SQLEXPRESS or MSSQL (unless you called it something else).


You can find connection string examples on this page:

http://www.connectionstrings.com/


Sounds like either SQL Browser isn't started or isn't configured to respond.

You didn't mention if you're using express or the full blown version... But that probably doesn't matter as the steps are pretty much the same:

http://www.symantec.com/connect/articles/install-and-configure-sql-server-2008-express

Assuming everything is actually installed, start with the "Enable TCP" section.


Serverfault is an excellent source of help on problems like this.

Try suggestions from threads like:

  • https://serverfault.com/questions/11745/i-cannot-connect-to-my-local-sql-server-2008
  • https://serverfault.com/questions/94923/a-network-related-or-instance-specific-error-occurred-while-establishing-a-connec
  • https://serverfault.com/questions/75048/sql-server-connection-issue
  • https://serverfault.com/questions/111789/how-to-connect-to-local-instance-of-sql-server-2008-express

Most of them suggest checking that the service is running, checking the firewall, checking the user, etc

There are lots of threads matching - search

Although it talks about sql express, this thread looks particularly helpful - https://serverfault.com/questions/191576/sql-server-cant-connect - lots of things to try

0

精彩评论

暂无评论...
验证码 换一张
取 消