Having problems connecting to SQL Server in C#

╄→尐↘猪︶ㄣ 提交于 2019-12-11 06:08:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!