c#2008 SQL Server Express连接字符串

Kir*_*ril 6 c# sql-server connection-string sql-server-2008-express

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

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

这是我的连接字符串的样子(私人信息被更改):

"Data Source="MACHINENAME\\SQLEXPRESS";User ID="Admin";Password="the_password";Initial Catalog="MyDatabase";Integrated Security=True;Connect Timeout=120");
Run Code Online (Sandbox Code Playgroud)

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

注意:

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

  2. 当我在同一台机器上时,连接字符串工作正常.

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

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

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

任何人都可以告诉我,如果我做错了什么?

更新,这是我的确切代码(这次只删除了密码,我添加了在同一台机器上运行的管理工作室的图片):

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");
Run Code Online (Sandbox Code Playgroud)

通过Management Studio连接:见图片http://s113.photobucket.com/albums/n202/ozpowermo/?action=view¤t=ManagementStudio.jpg

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

我想到了:

当使用"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";"
Run Code Online (Sandbox Code Playgroud)

eu-*_*-ne 12

从连接字符串中删除Integrated Security = True并(可选)添加Persist Security Info = True;

来自MSDN:

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