C# SqlConnection can't connect
I want to connect to an online SQL database. to do that, I use this code:
SqlConnection conn = new SqlConn开发者_StackOverflowection("Data Source=77.243.225.193;User ID=rune;Password=secret;database=test");
conn.Open();
Textbox.Text = "ServerVersion: " + conn.ServerVersion.ToString() + "\n\r";
Textbox.Text = "State: " + conn.State.ToString();
I just got the code from the internet, since this is my first time using this. Somehow this does not work because it says that it can't connect to the database. Can anyone explain me why?
Thanks!
Run throug a series of tests and check off the ones that past:
- Is 77.243.255.139 a valid address? Can you ping it?
- Does the Sql Server @ the IP address require a connection on a specific port, e.g. 77.243.255.139,1724?
- Is
test
a valid name of a database that is online and mounted on the server? - Is
rune
a valid SQL login for the server? - Does
rune
have the appropriate permissions to access thetest
database? - Is the password correct?
There are obviously a myriad of reasons why you might not be able to connect, what exact error message are you getting?
UPDATE: based on your last comment, you saying you use phpMyAdmin... Which is a management tool for MySql databases (someone correct me if I am wrong)?
If it is a MySql database, you need to be using MySql ADO.NET Connector along with a valid MySql connection string. Check http://www.mysql.com/products/connector/
If this is SQL Server Express (or Developer edition) then I assume remote connections using TCP/IP isn't enabled (that's the default). If you have access to the SQL Server then I suggestion you follow the instructions in this link: Configure SQL Server for remote connections. You can also try to telnet to port 1433 (default SQL Server port), using "telnet 77.243.255.139 1433" from command line.
Try
Data Source=77.243.225.193,1433;Network Library=DBMSSOCN;Initial Catalog=test;
User ID=rune;Password=secret;
The
Network Library=DBMSSOCN
tells the provider to use TCP/IP instead of named pipes.
I notice from your previous comment, that there's some confusion over the IP address and I've assumed that it's 77.243.225.193, but please find out the definitive address; you'll get nowhere if you don't!
精彩评论