views:

98

answers:

2

Hey guys,

I have an MS SQL Server 2005 Express running on a VPS. I'm using pymssql in Python to connect to my server with the following code:

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:

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();:

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. Can anyone point me out to what I'm missing here?

Thanks!

A: 

Let's say your host is localhost. When you are using SQLEXPRESS you need to specify that in your Instance Name.

Loke this: Data Source=localhost\SQLEXPRESS if it is bound to localhost. Otherwise it might just work with: Data Source=.\SQLEXPRESS.

If you have management studio installed you can fire that up and check what connection string it is using!

Filip Ekberg
Hey - thanks for the answer.I tried changing it to:SqlConnection myConnection = new SqlConnection("Data Source=host\\SQLEXPRESS,port;Network Library=DBMSSOCN; Initial Catalog=db;User ID=me;Password=pwd;");but still no luck (I get the same error). How exactly do I get the connection string from the management studio?
Aviv
Are you writing "host" or your actual host? When you open up management studio you see 3 fields, where the first is the server address, the second is the user and the third is the password. And you see there which IP the Server is bound too. ( If you can connect with management studio that is )
Filip Ekberg
Hey, yes I write my actual IP not the word host :)
Aviv
Is the server on your local machine? Can you access it with Management studios? If it is local, please try writing your computer name instead of the IP.
Filip Ekberg
A: 

Still doesn't work.. I'm able to connect to the remote server with 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. When I try to connect from the ASP.NET page, it just doesn't work - I get the error aforementioned. Can it have something to do with the company hosting my asp.net page? (GoDaddy) here is the code again..(assuming my host is 11.22.33.44 and my db is named 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

Aviv