none
Login name failed for the Databases RRS feed

  • Question

  • Dear All,

     

    I am just going to connect sql server express 2005 with this connection string and being able to do in vb.net but not being able to connect with C#. In C# its giving an error message "login name failed for the database..."

     

     

     

    Code Snippet

     Con = "Data Source=.//SQLEXPRES;Initial Catalogue=myDataBase;Trusted_Connection=True;"

     SqlConnection Con = new SqlConnection(Con);

    Con.Open();


     

     

    Can any body tell me the solution please as it is very important. I am really puzzled about it but finding no clue..

     

    Urgent help will be highly appreciated.

     

    Regards,

    Biswajit

    Wednesday, July 2, 2008 7:18 PM

Answers

  • To be honest the connection string parsing is a bit annoying with SqlConnection, it's not strict.

     

    So if you pass in a bogus connection string attribute, it does not complain, it just ignores it.

     

    Hence you think Trusted_Connection=Yes is working, but instead it is not working and you are attempting to log in with blank user id and password.  Bummer.   Also, Initial Catalogue you think is working too but no, it is Initial Catalog is correct (American spelling).

     

    Hence you have to be very very careful and use the proper syntax, search Internet for SqlConnection ConnectionString help topic.   If you want to avoid this issue altogether and use Intellisense, then try the nifty SqlConnectionStringBuilder class.

     

    This should work:

     

    SqlConnection conn = new SqlConnection(@"data source=.\sqlexpress;initial catalog=master;integrated security=true;");

    conn.Open();

     

    Also, you can use SqlConnectionStringBuilder class to be safer:

     

    SqlConnectionStringBuilder sc = new SqlConnectionStringBuilder();

    sc.DataSource = @".\sqlexpress";

    sc.InitialCatalog = "master";

    sc.IntegratedSecurity = true;

    MessageBox.Show(sc.ConnectionString);

    SqlConnection conn = new SqlConnection(sc.ConnectionString);

    conn.Open();

     

    Monday, July 7, 2008 6:41 PM

All replies

  • Try removing Trusted_Connection=True or setting it to Yes: Trusted_Connection=Yes

     

    Adam

     

    Wednesday, July 2, 2008 7:28 PM
  • Are you sure that connection strings in VB.NET and C# are identical? Did you post actual connection string here? The reason I am asking is because it does not matter if connection string is created in VB.NET or in C#. They will look the same and do not depend on language except escape characters, so if it works in VB.NET it will work in C#. It also looks like you have two variables (Con) with same name, but one for the connection string and another one for the actual connection. Is it typo?

    Thursday, July 3, 2008 10:02 AM
    Moderator
  • To be honest the connection string parsing is a bit annoying with SqlConnection, it's not strict.

     

    So if you pass in a bogus connection string attribute, it does not complain, it just ignores it.

     

    Hence you think Trusted_Connection=Yes is working, but instead it is not working and you are attempting to log in with blank user id and password.  Bummer.   Also, Initial Catalogue you think is working too but no, it is Initial Catalog is correct (American spelling).

     

    Hence you have to be very very careful and use the proper syntax, search Internet for SqlConnection ConnectionString help topic.   If you want to avoid this issue altogether and use Intellisense, then try the nifty SqlConnectionStringBuilder class.

     

    This should work:

     

    SqlConnection conn = new SqlConnection(@"data source=.\sqlexpress;initial catalog=master;integrated security=true;");

    conn.Open();

     

    Also, you can use SqlConnectionStringBuilder class to be safer:

     

    SqlConnectionStringBuilder sc = new SqlConnectionStringBuilder();

    sc.DataSource = @".\sqlexpress";

    sc.InitialCatalog = "master";

    sc.IntegratedSecurity = true;

    MessageBox.Show(sc.ConnectionString);

    SqlConnection conn = new SqlConnection(sc.ConnectionString);

    conn.Open();

     

    Monday, July 7, 2008 6:41 PM