c# 2008 SQL Server Express Connection String

这一生的挚爱 提交于 2019-11-28 07:57:53

问题


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";"

回答1:


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.




回答2:


try this

string sqlcon=("Data Source="your pc name\\SQLEXPRESS"; 
UserID=sa;
Password=****;
Initial Catalog="+MyDatabase+"; 
IntegratedSecurity=True;");



回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/997169/c-sharp-2008-sql-server-express-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!