6

I have a 2008 SQL Server Express installed on one of my machines and I'm attempting to establish a remote connection... when I use the MS SQL Server Management Studio I can log into the database without any problems at all (with the same credentials), but when I try to create a connection string in my C# application I get an exception:

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.

Here is what my connection string looks like (the private information is changed):

"Data Source="MACHINENAME\\SQLEXPRESS";User ID="Admin";Password="the_password";Initial Catalog="MyDatabase";Integrated Security=True;Connect Timeout=120");

As I said, I can login using the Management Studio with the same settings: same user id, password and data source name, but it fails when I attempt to open a connection with the above connection string.

Note:

  1. I have enabled the remote connectivity on the server, disabled the firewall, enabled TCP/IP connection to the server, turned on the SQL Browser.

  2. The connection string works fine when I'm on the same machine.

  3. I looked up the Integrated Security option and I set it to false just to make sure that it's not attempting to use the Windows Login, but it still fails.

  4. The database is setup to allow both windows login and database login.

  5. I changing the Integrated Security option to SSPI, True, and finally False, all 3 gave me the same error as above.

Can anybody tell me if I'm doing something wrong?

UPDATE, here is my exact code (this time only the password is removed, and I've added a picture of management studio running on the same machine):

string _connectionString =
            //string.Format("Server=%s;User ID=%s;Password=%s;Database=%s;Connect Timeout=120", // Same problem
            //string.Format("Server=%s;User ID=%s;Password=%s;Database=%s;Integrated Security=False;Connect Timeout=120", // Same problem
            string.Format("Data Source=%s;User ID=%s;Password=%s;Initial Catalog=%s;Integrated Security=False;Connect Timeout=120", // Same problem
            "GANTCHEVI\\SQLEXPRESS",
            "FinchAdmin",
            "the_password",
            "Finch");

Connected Via Management Studio: See Picture http://s113.photobucket.com/albums/n202/ozpowermo/?action=view&current=ManagementStudio.jpg

http://s113.photobucket.com/albums/n202/ozpowermo/?action=view&current=ManagementStudio.jpg

I FIGURED IT OUT:

When using the "Data Source=" label one should use the "User Id", if you use User ID it doesn't seem like it works!

string _connectionString = "Data Source=GANTCHEVI\\SQLEXPRESS;Initial Catalog=Finch;Integrated Security=False;User Id=FinchAdmin;Password=the_password;Connect Timeout=0";"
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
Kiril
  • 39,672
  • 31
  • 167
  • 226

5 Answers5

12

Remove Integrated Security=True from your connection string and (optional) add Persist Security Info=True;

From MSDN:

Integrated Security - When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

eu-ge-ne
  • 28,023
  • 6
  • 71
  • 62
2

try this

string sqlcon=("Data Source="your pc name\\SQLEXPRESS"; 
UserID=sa;
Password=****;
Initial Catalog="+MyDatabase+"; 
IntegratedSecurity=True;");
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
LZara
  • 385
  • 3
  • 5
1

Ok - I am guessing you have tried all of these http://blogs.msdn.com/sqlexpress/archive/2005/05/05/415084.aspx

Have you tried sqlping on MACHINENAME\SQLEXPRESS

Can you ping MACHINENAME

Finally, I am pretty sure you only need 1 slash i.e. MACHINENAME\SQLEXPRESS

David E
  • 415
  • 4
  • 11
  • Thanks for replying :) Yep, I already enabled all of those settings and I can connect to the DB using the Management Studio, I am actually connected with Management Studio right now. I can ping MACHINENAME and it returns 192.168.0.9 (the address of the machine connected to my router). It's two slashes when it's inside a string since the slash is considered a special character. – Kiril Jun 15 '09 at 17:12
  • Yes completly correct on two slashes, thought it was in the config. Doh! – David E Jun 16 '09 at 10:16
1

It could be your SQL instance is not cobfigured to accept incoming TCP connections, you can check this under Start-> SQL Server 2008 -> Conguration Tools -> SQL Server Configuration Manager. On the left side in that tool you'll see network configuration, expand it to see which protocols are enabled.

Colin
  • 10,630
  • 28
  • 36
  • It's configured to accept TCP connections. I have Management Studio on the same machine as my C# application and I am connected to the DB with the same settings. – Kiril Jun 15 '09 at 17:14
  • 1
    I wanted to have Integrated Security on my local db server so that my current Windows account credentials could be used. This answer got me to open up the Configuration Tools. When I started the SQL Server Agent, I could connect from my C# app to a database. Thanks Colin. – DavidHyogo Aug 14 '12 at 06:33
0

What if you use Integrated Security=SSPI?

If you are not wanting to use a windows login I believe the other answer about removing the Integrated Security parameter is correct.

Chrisb
  • 729
  • 1
  • 5
  • 11