Broken_Code
Broken_Code

Reputation: 306

How to connect to sql server on c# application

I can connect asp.net applications to my databases in SQL Server 2008, however when I try to do the same from Visual Studio in a Winforms application, I always get the error:

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

I use the connection string:

 SqlConnection Conn = new SqlConnection(@"Server=.\\myserver address\user;Database=testDatabase");

and I get an error when my form loads as that is when I start the connect.

I have gone to my SQL Server Configuration Manager and I get this:

enter image description here

So I do not know what I am doing wrong. There are previous files for older versions of SQL Server in the program files however I do not see how they could affect it...

My sql services

enter image description here

I would also like to add that I added sql server to the firewall exceptions already

Upvotes: 2

Views: 2834

Answers (3)

Marek
Marek

Reputation: 3575

You should consider using connection with following syntax

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;Integrated Security=SSPI;

OR

Server=myServerName\myInstanceName;Database=myDataBase;Integrated Security=true;

When using current Windows account credentials

OR on local Server with default Windows authentication

Server=myServerName;Database=myDataBase;Integrated Security=true

(this was solution that worked for OP)

And for @"Server=.\\myserver using @ before string means that no character would escape that string so

\\ without @ turns into \

\\ with @ stays \\

check C# '@' before a String

For more information about connection strings please visit connectionstrings.com

Upvotes: 1

sdrzymala
sdrzymala

Reputation: 387

Please try to use the folowing command: I can see that you may have a problem with MOF.

mofcomp.exe "C:\Program Files (x86)\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"

Upvotes: 0

coder
coder

Reputation: 13250

Check whether these services has been started or not as shown in below snapshot

enter image description here..

If then try to start them and please try to connect your database.

Upvotes: 0

Related Questions