简体   繁体   English

无法从ASP.NET C#页连接到SQL Server 2005 Express

[英]Can't connect to SQL Server 2005 Express from an ASP.NET C# page

I have an MS SQL Server 2005 Express running on a VPS. 我在VPS上运行了MS SQL Server 2005 Express。 I'm using pymssql in Python to connect to my server with the following code: 我在Python中使用pymssql通过以下代码连接到服务器:

conn = pymssql.connect(host='host:port', user='me', password='pwd', database='db')

and it works perfectly. 而且效果很好。 When I try to connect to the server from an ASP.NET C# page with the following code: 当我尝试使用以下代码从ASP.NET C#页连接到服务器时:

SqlConnection myConnection = new SqlConnection("Data Source=host,port;Network Library=DBMSSOCN; Initial Catalog=db;User ID=me;Password=pwd;");

myConnection.Open();

When I run the ASP.NET page I get the following exception at myConnection.Open();: 当我运行ASP.NET页时,在myConnection.Open();处收到以下异常:

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. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

I tried restarting the SQL Server but I had no luck. 我尝试重新启动SQL Server,但是没有运气。 Can anyone point me out to what I'm missing here? 谁能指出我在这里想念的东西吗?

Thanks! 谢谢!

Let's say your host is localhost. 假设您的主机是本地主机。 When you are using SQLEXPRESS you need to specify that in your Instance Name. 使用SQLEXPRESS时,需要在实例名称中指定。

Loke this: Data Source=localhost\\SQLEXPRESS if it is bound to localhost. 对此:如果Data Source=localhost\\SQLEXPRESS绑定到本地主机,则为: Otherwise it might just work with: Data Source=.\\SQLEXPRESS . 否则,它可能只适用于: Data Source=.\\SQLEXPRESS

If you have management studio installed you can fire that up and check what connection string it is using! 如果您安装了Management Studio,则可以启动并检查它使用的连接字符串!

Still doesn't work.. I'm able to connect to the remote server with SQL Management Studio. 仍然无法正常工作。我可以使用SQL Management Studio连接到远程服务器。 I enter host,port\\SQLEXPRESS (of course I my actual IP number as the host and my actual port) in the Server Name field, select SQL Security and enter my username and password and it works perfectly. 我在“服务器名称”字段中输入host,port \\ SQLEXPRESS(当然,我的实际IP地址为主机和我的实际端口),选择SQL Security,然后输入我的用户名和密码,即可正常运行。 When I try to connect from the ASP.NET page, it just doesn't work - I get the error aforementioned. 当我尝试从ASP.NET页进行连接时,它不起作用-我收到了上述错误。 Can it have something to do with the company hosting my asp.net page? 它与托管我的asp.net页的公司有关系吗? (GoDaddy) here is the code again..(assuming my host is 11.22.33.44 and my db is named bla (GoDaddy)再次是代码。(假设我的主机是11.22.33.44,并且我的数据库名为bla

string connectString = @"Data Source=11.22.33.44,1433\SQLEXPRESS;Network Library=DBMSSOCN;Initial Catalog=bla;User ID=username;Password=pwd;";
SqlConnection myConnection = new SqlConnection(connectString);
myConnection.Open();

Thanks again 再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM