Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL database connection string not working 1

Status
Not open for further replies.

ccampbell

Programmer
Aug 16, 2001
201
US
Can anyone help me understand why this connection string is not working. I have the following piece of code

string connString = "user id=sa;password=;initial catalog=dbName;data source=Test;Connect Timeout=30";
SqlConnection connect = new SqlConnection(connString);

try
{
connect.Open();
.....}
catch
{
...
}

It gives me the error "SQL server does not exist or access denied." The datasource is correct and tests out correctly in the ODBC administrator. It does not matter what database name I put in there. Another interresting thing, If I change the connection string and eliminate the data source completely it give me the error
"Login failed for user "sa" Not associated with a trusted SQL server connection" I have used the ODBC connection and database in my C++ application and they work just fine. Any ideas why C# is causing me such a headache? Thanks for any help.
 
Try leaving the password section off, since it's blank.

Which, BTW, leaves you vulnerable to a virus that attacks SQL servers. You should assign a password to your sa accounts.

Chip H.
 
Thanks Chip H. but that did not work. I have tried taking all authentication off and it still will not work. I think that I have tried every variation of connection string that I can think of and I still get one of the two errors when trying to open a connection. I think that I am missing something either in my SQL installation or in my .NET installation. The same connection string will work with my ASP and C++ applications though, why will they not work with C#?
 
Take a look at:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopic.htm

to make sure your string is formatted correctly. Hint: don't specify "Integrated Security" if you're using a username & password.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top