Take the 2-minute tour ×

I am trying to connect to the sample database Northwind in SQL Server Express 2005 through Visal C++ 2008 using the following code:

SqlConnection^ con=gcnew SqlConnection();
con->ConnectionString="Data Source=localhost\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI";

SqlCommand^ com=gcnew SqlCommand();
com->CommandText="Select * From Customers";


    SqlDataReader^ myReader;


catch(Exception^ e)

But I am getting the exception that

login to the database failed for the user PC-ANKIT

And after this, I have no idea what happens, but the the tables in the database magically disappear. The list of databases in the SQL Server Management Studio still shows the Northwind database, but it does not contain any tables, views or anything.

Also, when I try doing the same thing using an equivalent code in VB in Visual Web Developer 2008 express edition - no problem arises whatsoever! I am able to connect to the database and access the data in all the tables.

Can anyone please help me resolve this problem? Thanks in advance!

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Try connecting to the database using visual studio's server explorer. If you connect successfully, you can right-click the connection and check out the properties. In there you will find the connection string - use that in your application and check out if it works.

Notice that this is a trusted connection, if you want to use username and password I use:

Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Integrated Security=SSPI;

Hope it helps!

share|improve this answer
thanks for the answer! But I still dont understand why the connection string I was using previously does not work in the case of VC++, as it works when I use it in Visual Web Developer –  ankit0311 Feb 29 '12 at 4:13
add comment

Your connectionstring ConnectionString="Data Source=localhost\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"; indicates that you try to access it with the "integrated security" which uses the currently logged in windows user to authenticate against the database. Maybe such user credentials don't exist for your databse yet. You can provide explicit credentials using this syntax:

Data Source=ServerName; Initial Catalog=DatabaseName; User Id=UserName; Password=UserPassword;
share|improve this answer
add comment

You need to make sure user PC-ANKIT is at least a db_datareader in your selected database (not just a login in SQL server). You can do that in SQL Management [your database] -> Security -> Users. Your user (or a group it belongs to) must be listed under this node. For this user (or group) Properties -> General -> Database Role Membership make sure db_datareader (or db_owner) is selected.

If you still have problems try using user and password authentication. Remember to enable mixed security mode in SQL Server -> Properties -> Security -> SQL Server and Windows Authentication Mode. You need to change your connection string in this case by replacing Integrated Security=SSPI; with User ID=your_user;Password=pass;

Make sure you use exactly the same connection string in VB version to rule out any differences between those 2 scenarios.

share|improve this answer
add comment

Your Answer


