Tuesday, February 14, 2012

Cannot connect to MSDE from C# code

Hello everybody and happy holidays to all.
I'm new to C#, and I appologize for this basic question.
I am trying to connect to an SQL database from a very short C# program, which I found on the Microsoft site, demonstrating this subject.
Here is my setup:
Windows 2000 Professional
dotNET Framework 1.1 with sp1 installed
MSDE 2000
Note: I do not own nor do I have Visual Studio.NET of any kind installed.

Now, here is what works:
osql -E -S localhost\vsdotnet
1>
And I can create and populate a database, which I called mydb.

Now, in my simple C# demo program I have this line

SqlConnection mycon = new SqlConnection("server=localhost;database=mydb;trusted_connection=true");

And the program compiles OK using "csc" command. But when I run it, I get this message:
Couldn't Open Connection to server=localhost;database=mydb;trusted_connection=true

Could anybody comment on what am I missing?
Thanks.The complete answer can't be given without knowing if you chose to use intgrated NT security or not during the MSDE installation. Either which way your connection string is incomplete.

If you installed MSDE using NT integrated security your connection string should also contain: Persist Security Info=False;Integrated Security=SSPI;

If not you will have to provide a username password pair such as:
uid=sa;pwd=secret;

Hope this helps

Cheers

Gary

No comments:

Post a Comment