c# 2008 SQL Server Express 连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/997169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# 2008 SQL Server Express Connection String
提问by Kiril
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:
笔记:
I have enabled the remote connectivity on the server, disabled the firewall, enabled TCP/IP connection to the server, turned on the SQL Browser.
The connection string works fine when I'm on the same machine.
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.
The database is setup to allow both windows login and database login.
I changing the Integrated Security option to SSPI, True, and finally False, all 3 gave me the same error as above.
我在服务器上启用了远程连接,禁用了防火墙,启用了到服务器的 TCP/IP 连接,打开了 SQL 浏览器。
当我在同一台机器上时,连接字符串工作正常。
我查找了集成安全选项并将其设置为 false 只是为了确保它没有尝试使用 Windows 登录,但它仍然失败。
数据库设置为允许 Windows 登录和数据库登录。
我将集成安全选项更改为 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");
通过管理工作室连接:见图片 http://s113.photobucket.com/albums/n202/ozpowermo/?action=view¤t=ManagementStudio.jpg
http://s113.photobucket.com/albums/n202/ozpowermo/?action=view¤t=ManagementStudio.jpg
http://s113.photobucket.com/albums/n202/ozpowermo/?action=view¤t=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=”标签时,应该使用“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";"
采纳答案by eu-ge-ne
Remove Integrated Security=True from your connection string and (optional) add Persist Security Info=True;
从您的连接字符串中删除 Integrated Security=True 并(可选)添加 Persist Security Info=True;
From MSDN:
来自 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 帐户凭据进行身份验证。
回答by Chrisb
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 登录,我相信有关删除集成安全参数的另一个答案是正确的。
回答by David E
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
回答by Colin
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 连接,您可以在开始-> SQL Server 2008 -> 配置工具 -> SQL Server 配置管理器下检查。在该工具的左侧,您将看到网络配置,展开它以查看启用了哪些协议。
回答by LZara
try this
尝试这个
string sqlcon=("Data Source="your pc name\SQLEXPRESS";
UserID=sa;
Password=****;
Initial Catalog="+MyDatabase+";
IntegratedSecurity=True;");