0

I am trying to connect to the sample database Northwind in SQL Server 2005 Express through Visual 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->Connection=con;
com->CommandText="Select * From Customers";

try
{
    con->Open();

    SqlDataReader^ myReader;
    myReader=com->ExecuteReader();
    myReader->Read();

    Console::WriteLine(myReader->GetData(5));

    myReader->Close();
}
catch(Exception^ e)
{
    Console::WriteLine(e->Message);
}
finally
{
    con->Close();
}

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ankit0311
  • 735
  • 3
  • 10
  • 20

3 Answers3

2

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!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TCS
  • 5,790
  • 5
  • 54
  • 86
  • 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 04:13
2

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;
tobsen
  • 5,328
  • 3
  • 34
  • 51
1

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.

Maciej
  • 7,871
  • 1
  • 31
  • 36