Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I set up a SQL Server Express on Windows Server 2008 and created some database & tables on it. I used a .udl file to test connectivity to it remotely and it's successful:
In .udl file, the SQL server shown is <my server="">/SQLEXPRESS, user name & password to login to server are: sa & <my password="">.

Then, I wrote a C# program to do connection. The first connection fails, but its parameters are totally as the ones used in .udl connectivity:

C#
SqlConnection conn = new SqlConnection("Data Source=STAN-MTP-SQL\\SQLEXPRESS; Database=MTP_TESTDB1;Integrated Security=true;User Id=sa;Password=<My password>");
try { conn.Open(); }


This kind of connection way appears at lots of sample programs in web. But it failed. The error message is: "ClientConnectionId:af804f7a-8485-4cc3-babb-le3b32546c92 Exception caught."

So, the two ways (.udl & C# program) used the same parameters to connect remotely. The test situation are the same. How come .udl succeeded but C# program failed?
Posted
Comments
Arjsrya 5-Jan-15 2:52am    
Try this.

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; Database=MTP_TESTDB1;User Id=sa;Password=<my password="">");
CHill60 5-Jan-15 2:57am    
That error message looks like one you've written into the catch clause of that try - what is the actual exception message? Hint - instead of "Exception caught" try using ex.Message
Stan Huang 5-Jan-15 3:01am    
I used ex.Message and the error message shown is "User 'sa' login failed" Except caught.
CHill60 5-Jan-15 3:07am    
You have both Integrated Security=true AND a userid and password in your connection - use one or the other not both. Also I've never seen User and Password capitalised - might make a difference
Stan Huang 5-Jan-15 2:58am    
Arjsrya, same result.

It works: SqlConnection conn = new SqlConnection(@"Server=192.168.8.98\SQLEXPRESS; Database=MTP_TESTDB1;User Id=sa;Password=stan");

I mistakenly added "\\" before 192.168.8.98
 
Share this answer
 
This works too and I think it's better:
SqlConnection conn = new SqlConnection(@"Server=STAN-MTP-SQL\SQLEXPRESS; Initial Catalog=MTP_TESTDB1;User Id=sa;Password=stan");

This doesn't work:
SqlConnection conn = new SqlConnection(@"Server=STAN-MTP-SQL\SQLEXPRESS; Initial Catalog=MTP_TESTDB1;Integrated Security=true");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900