0

I'm trying to connect to the database on my server with my C# program, and the connection keeps timing out. my connection string is:

string MyConnection = @"Data Source=YJN-DC-SQLEXPRESS;Initial Catalog=Test; Integrated Security = SSPI; User ID= username; Password=password;";

SQL Server does allow remote connections. I don't got the reputation to post image so here's the link to it http://imgur.com/0CDBg5q this is the error message I get. I'm using Visual Studio 2012

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
GK28
  • 81
  • 3
  • 13
  • 6
    Probably the connection string should be something like: `@"Data Source=YJN-DC\SQLEXPRESS...` You need a backslash between the computer name and the SQLEXPRESS instance name – Steve May 26 '15 at 18:16
  • 5
    Also you should use either `Integrated Security=SSPI` (Windows authentication) or `User ID= username; Password= password` (SQL authentication), not both of them – dotnetom May 26 '15 at 18:21
  • [Difference between Integrated Security = True and Integrated Security = SSPI](http://stackoverflow.com/questions/1229691/) – Steve May 26 '15 at 18:28
  • How, do i mark this as answered? also 1 more thing, which is correct? "Password=password" or "Password = password;" – GK28 May 26 '15 at 19:00

1 Answers1

0

You need to make sure that your connection string is correct. For starters, as dotnetom pointed out you need to either use Integrated Security (Windows Authentication) or User ID and Password, not both.

Here's some good information for building connection strings, but I'll give you the basics for what you need. https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx

If you are wanting to use a username and password, then you will need to use:

String MyConnection = @"Data Source=<ServerName>;InitialCatalog=<DatabaseName>;User ID=<UserName>;Password=<Password>;";

if you are wanting to use integrated security (Windows Authentication) then you will need to use:

String MyConnection = @"Data Source=<ServerName>;InitialCatalog=<DatabaseName>;Integrated Security=True;";

Replace <ServerName> and all the others with the information you have. If the server has an instance name, then you will need that in the <ServerName>. The result would be similar to ServerName\InstanceName. As long as you are authenticating with windows authentication then you should be able to use Integrated Security=True and be okay.

If you do in fact need to increase the timeout, you can do so by adding a connection timeout: Connection Timeout=30. The connection timeout is in seconds.

If you have Microsoft SQL Server Management Studio, I would highly suggest you ensure that your credentials work with the server in question. Try out the username/password combination or the integrated security.

Edit: To answer your question about whether it's Password= Password or Password=Password, you should go with the 2nd option and remove the spaces between. The other way I believe should still work, it's just a little more concise without the spaces.

Here's another great site with example connection strings: Example Connection Strings

Community
  • 1
  • 1
Death259
  • 153
  • 2
  • 14