c# 2008 SQL Server 快速连接字符串

标签 c# sql-server connection-string sql-server-2008-express

我在我的一台机器上安装了 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.

这是我的连接字符串的样子(私有(private)信息已更改):

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

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

注意:

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

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

  3. 我查看了“集成安全”选项并将其设置为 false,以确保它不会尝试使用 Windows 登录,但它仍然失败。

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

  5. 我将 Integrated Security 选项更改为 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");

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

我想通了:

当使用“Data Source=”标签时,应该使用“User Id”,如果你使用 User ID,它似乎不起作用!

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

最佳答案

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

来自 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.

关于c# 2008 SQL Server 快速连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/997169/

相关文章:

c# - 是否可以在 net core 中加载特定于平台的 .net 库?

c# - Console.ReadLine() 方法的完整规范是什么?

c# - 计算连续休假天数,跳过节假日和周末

sql-server - DTS 断开连接编辑中列的数据类型

mysql - 如何在sql查询中添加同一组的最后一个值

c# - 如何在asp.net中单击一次后设置超链接过期

sql - 每小时选择 1 行

asp.net - 将连接字符串保留在配置文件中有多安全

.net - 如何将连接字符串从 appsettings.[name].json 传递到 .NET Core 中的 DbContext?

asp.net - 我可以从 web.config 读取所有连接字符串而不需要每个连接字符串的名称吗? ASP.net