c#2008 SQL Server Express连接字符串 [英] c# 2008 SQL Server Express Connection String

查看:199
本文介绍了c#2008 SQL Server Express连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


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

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

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

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.

注意:


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

  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.

当我在服务器上时,连接字符串可以正常工作

The connection string works fine when I'm on the same machine.

我查找了集成安全性选项,并将其设置为false只是为了确保它不尝试使用Windows登录名,但是仍然失败。

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.

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

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

我将Integrated Security选项更改为SSPI,True,最后是False,这3个都给了我与上面相同的错误。

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?

更新,这是我的确切代码(这次仅删除了密码,我ve添加了在同一台计算机上运行的Management Studio的图片):

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

通过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

我已经弄清楚了:

使用数据源=标签时,如果使用,则应使用用户ID用户ID似乎不起作用!

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


推荐答案

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

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

从MSDN:

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

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 Express连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆