简体   繁体   English

在C#中连接到SQL Server时遇到问题

[英]Having problems connecting to SQL Server in C#

I am trying to connect to a SQL Server. 我正在尝试连接到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 我正在尝试在C#中使用SqlConnection对象,我曾尝试将域放在用户名之前,并且尝试使用和不使用端口号

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 我没有收到任何错误消息,它只是挂在myConnection.Open

You try connection string in this format. 您尝试使用这种格式的连接字符串。 Please find this link for more info about connection string in c# link 请在C# 链接中找到此链接以获取有关连接字符串的更多信息。

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

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; 正如@marc_s在注释中已经提到的那样,必须在连接逗号时指定端口号,在这种情况下,它应该类似于server=ipaddress,portnumber; .

However, these steps may help you to quickly troubleshoot connection issue with SQL Server 但是, 这些步骤可以帮助您快速解决SQL Server的连接问题

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

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