1

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

jarlh
  • 42,561
  • 8
  • 45
  • 63
Joe BW
  • 113
  • 4
  • 14
  • 1
    Try either just `"server=ipaddress;"`, or if that doesn't work, try `"server=ipaddress,portnumber;"` - separate IP address and port number by a **comma** (`,`), not a colon ... – marc_s Aug 06 '19 at 10:50
  • You have `Trusted_Connection` set to `yes` but also pass values for the username and password. It's one of the other; which are you really trying to use? – Thom A Aug 06 '19 at 10:50
  • @Larnu wouldn't that be `Integrated Security=True`? – Filburt Aug 06 '19 at 10:52
  • `Integrated Security` and `Trusted_Connection` are synonyms, @Filburt: [What is the difference between Trusted_Connection and Integrated Security in a connection string?](https://stackoverflow.com/a/3077529/3484879) – Thom A Aug 06 '19 at 10:53
  • I've removed the Trusted_Connection line as I would like to connect with username and password. I have also tried Marcs suggestions, however it still reacts the same way and hangs when trying to open connection – Joe BW Aug 06 '19 at 10:55
  • why to use Trusted_Connection and use password too,usually I open management studio and copy paste server name like server3\sql2014 or 10.10.10.10\sql2014 – Mahmoud Nasr Aug 06 '19 at 10:57
  • Sounds like the host/application can't reach the specified instance. if it's hanging then it implies you're getting a timeout error, rather than an invalid credentials error. I *assume* TCP/IP is configured and enabled on the remote host, that the firewall port(s) are open (at least to the local host), and that the IP/port is correct in your connection string. – Thom A Aug 06 '19 at 10:58
  • @Larnu Cool, thanks for the reference! Guess since i started relying on the [Data Link Wizard](https://stackoverflow.com/a/10480011/205233) I stopped to care about the nitty-gritty stuff. – Filburt Aug 06 '19 at 11:01
  • try connection string like this Data Source=ipaddress:portnumber;Initial Catalog= databaseName;User ID= username;Password= xxxxx;Integrated Security=False;connection timeout=30; – Shivani Aug 06 '19 at 11:03
  • @Shivani still no luck – Joe BW Aug 06 '19 at 11:24
  • @JoeBW Give the [Data Link Wizard](https://stackoverflow.com/a/10480011/205233) a try - it will allow you to troubleshoot your connection issue outside your application and provide a working Connection String once you got it working there. – Filburt Aug 06 '19 at 12:37

2 Answers2

0

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;");
DarkRob
  • 3,843
  • 1
  • 10
  • 27
0

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

Shekar Kola
  • 1,287
  • 9
  • 15