c#2008 SQL Server Express连接字符串

时间:2021-08-01 15:53:17

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:

我在我的一台机器上安装了一个2008 SQL Server Express,我正在尝试建立远程连接...当我使用MS SQL Server Management Studio时,我可以毫无问题地登录数据库(使用相同的凭证),但是当我尝试在C#应用程序中创建连接字符串时,我得到一个异常:

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.

建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。

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.

正如我所说,我可以使用具有相同设置的Management Studio登录:相同的用户ID,密码和数据源名称,但是当我尝试使用上述连接字符串打开连接时,它会失败。

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.

    我已启用服务器上的远程连接,禁用防火墙,启用与服务器的TCP / IP连接,打开SQL浏览器。

  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.

    我查找了Integrated Security选项并将其设置为false只是为了确保它没有尝试使用Windows登录,但它仍然失败。

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

    数据库设置为允许Windows登录和数据库登录。

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

    我将集成安全性选项更改为SSPI,True,最后是False,所有3都给了我与上面相同的错误。

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

通过Management Studio连接:见图片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!

当使用“Data Source =”标签时,应使用“用户ID”,如果您使用用户ID,它似乎不起作用!

string _connectionString = "Data Source=GANTCHEVI\\SQLEXPRESS;Initial Catalog=Finch;Integrated Security=False;User Id=FinchAdmin;Password=the_password;Connect Timeout=0";"

5 个解决方案

#1


11  

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

从连接字符串中删除Integrated Security = True并(可选)添加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.

集成安全性 - 如果为false,则在连接中指定用户ID和密码。如果为true,则使用当前Windows帐户凭据进行身份验证。

#2


2  

try this

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

#3


1  

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

好的 - 我猜你已经尝试了所有这些http://blogs.msdn.com/sqlexpress/archive/2005/05/05/415084.aspx

Have you tried sqlping on MACHINENAME\SQLEXPRESS

你有没有尝试过在MACHINENAME \ SQLEXPRESS上进行sqlping

Can you ping MACHINENAME

你能ping MACHINENAME吗?

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

最后,我很确定你只需要1个斜杠即MACHINENAME \ SQLEXPRESS

#4


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.

可能是您的SQL实例未配置为接受传入的TCP连接,您可以在Start-> SQL Server 2008 - > Conguration Tools - > SQL Server Configuration Manager下进行检查。在该工具的左侧,您将看到网络配置,将其展开以查看启用了哪些协议。

#5


0  

What if you use Integrated Security=SSPI?

如果您使用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.

如果您不想使用Windows登录,我相信关于删除Integrated Security参数的另一个答案是正确的。

#1


11  

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

从连接字符串中删除Integrated Security = True并(可选)添加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.

集成安全性 - 如果为false,则在连接中指定用户ID和密码。如果为true,则使用当前Windows帐户凭据进行身份验证。

#2


2  

try this

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

#3


1  

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

好的 - 我猜你已经尝试了所有这些http://blogs.msdn.com/sqlexpress/archive/2005/05/05/415084.aspx

Have you tried sqlping on MACHINENAME\SQLEXPRESS

你有没有尝试过在MACHINENAME \ SQLEXPRESS上进行sqlping

Can you ping MACHINENAME

你能ping MACHINENAME吗?

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

最后,我很确定你只需要1个斜杠即MACHINENAME \ SQLEXPRESS

#4


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.

可能是您的SQL实例未配置为接受传入的TCP连接,您可以在Start-> SQL Server 2008 - > Conguration Tools - > SQL Server Configuration Manager下进行检查。在该工具的左侧,您将看到网络配置,将其展开以查看启用了哪些协议。

#5


0  

What if you use Integrated Security=SSPI?

如果您使用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.

如果您不想使用Windows登录,我相信关于删除Integrated Security参数的另一个答案是正确的。