在C#中连接到SQL Server时遇到问题 [英] Having problems connecting to SQL Server in C#

查看:91
本文介绍了在C#中连接到SQL Server时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到SQL Server.

I am trying to connect to a SQL Server.

我试图在C#中使用 SqlConnection 对象,我尝试将域放在用户名之前,并且尝试使用端口号和不使用端口号

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)
{
}

我没有收到任何错误消息,它只是挂在 myConnection.Open

I don't get any error message, it just hangs at myConnection.Open

推荐答案

正如@marc_s的注释中已经提到的那样,必须在逗号中指定端口号,在您的情况下,它应该 server = ipaddress,portnumber; .

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;.

但是,这些步骤可以帮助您快速解决SQL Server的连接问题

However, these steps may help you to quickly troubleshoot connection issue with SQL Server

这篇关于在C#中连接到SQL Server时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆