问题
I am trying to connect to a SQL Server.
I am trying to use the SqlConnection
object in C#, I've tried putting a domain before my username and I've tried with and without the portnumber
SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=xxxxx;" +
"server=ipaddress:portnumber;" +
"Trusted_Connection=yes;" +
"database=databaseName; " +
"connection timeout=30");
try
{
myConnection.Open();
}
catch(Exception ex)
{
}
I don't get any error message, it just hangs at myConnection.Open
回答1:
You try connection string in this format. Please find this link for more info about connection string in c# link
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
SqlConnection myConnection = new SqlConnection("Data Source=ipaddress;
Initial Catalog=dbname;
User Id=userid;
Password=pass;");
回答2:
As mentioned already in comments by @marc_s, port# must be specified in connection with comma, in your case it should like server=ipaddress,portnumber;
.
However, these steps may help you to quickly troubleshoot connection issue with SQL Server
来源:https://stackoverflow.com/questions/57374313/having-problems-connecting-to-sql-server-in-c-sharp