locked
error in opening connection to sql! RRS feed

  • Question

  • hey everyone...tnx for notice...

    this error is for what??  why i got this error??

    & how can i fix it?!

    Cannot open database "DBTest1" requested by the login. The login failed.
    Login failed for user 'Bahador-gh-PC\Bahador-gh'.

    my windows is 7...& i coded in visual C# Express 2010..also i had this issue with C# Express 2008...

    my code is here:

        SqlCommand sqComm;
        SqlDataAdapter sqDA;
        DataSet ds = new DataSet();
    
        SqlConnection sqConn = new SqlConnection(@"Data Source =.\SQLEXPRESS;Initial Catalog = DBTest1;Integrated Security= True");
    
        private void RegButton_Click(object sender, EventArgs e)
        {
          string name, family;
          int phone;
    
          name = Convert.ToString(NameBox.Text);
          family = Convert.ToString(FamilyBox.Text);
          phone = Convert.ToInt32(PhoneBox.Text);
    
          /* error in this line
           * Cannot open database "DBTest1" requested by the login. The login failed.
            Login failed for user 'Bahador-gh-PC\Bahador-gh'.*/
          sqConn.Open();
    
          sqComm = new SqlCommand("INSERT INTO PersonDetail(Name,Family,Phone) Values(@name,@family,@phone)");
    
          sqDA = new SqlDataAdapter(sqComm);
    
          sqDA .Fill(ds,"PersonDetail");
    
          foreach (DataRow row in ds.Tables[0].Rows)
          {
            Console.WriteLine("{0} - {1} {2}", row["Name"], row["Family"], row["Phone"]);
          }
    
          sqConn.Close();
        }
    
    • Moved by David M Morton Friday, November 12, 2010 11:01 PM More of a SQL question than a C# question (From:Visual C# General)
    Friday, November 12, 2010 10:16 PM

Answers

  • Hello, thanks for your post.


    You're using Integrated Security in your Connection String:

      SqlConnection sqConn = new
     SqlConnection(@"Data Source =.\SQLEXPRESS;Initial Catalog = DBTest1;Integrated Security= True"
    );

    But, the user doesn't have access to SQL Server as a Login. Please try following these steps to add that user as SQL Server Login:

    1. Open Microsoft SQL Server Management Studio
    2. Go to Security Section
    3. Right-Click on Logins , then click on New Login...
    4. On the "Login - New" window, please click the "Search..." button
    5. On the "Select User or Group" window you should enter your user name, if you don't know it completely write some text of the name and then click Check Names , after your find the user "Bahador-gh-PC\Bahador-gh" or another one, please click OK.
    6. Check "Windows Authentication" Radio Button
    7. On the left panel, click Server Roles select sysadmin
    8. On the left panel, click User Mapping , check the Box on the left side of your Database (DBTest1), that will create an user on that Database for your new Login.


    After doing that, please try connecting to SQL Server again from your C# Application.


    Hope this helps.


    Willy Taveras.-

    http://itlearn.net

    Sunday, November 14, 2010 3:13 PM

All replies

  • It's a login failure.  You don't have permission to access the database.  You need to use Sql Server Management Studio to set up a login for yourself on the database.  I'm moving this thread to the SQL forums so that they can help you further. 
    Hello.
    Friday, November 12, 2010 11:01 PM
  • so how can i access my Express database?

    it must have a way...am i right??

    tnx for doing this:)

    Friday, November 12, 2010 11:07 PM
  • no one can't answer me?

    plz/...... i need it seriously :((

    Saturday, November 13, 2010 4:09 PM
  • Use UDL file in order to get the connection string.

    Please see my post here:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ceb44693-a0c6-41dd-9742-6bf8eebf36b7/7b2e1bc2-72d7-488d-8a18-7837bb9dd46b/

    Noam B.

     



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Sunday, November 14, 2010 12:01 PM
  • Connect as Admin to SSMS and grant to that user access to the database...

    Probably you need to map the user with login


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Sunday, November 14, 2010 1:43 PM
  • Hello, thanks for your post.


    You're using Integrated Security in your Connection String:

      SqlConnection sqConn = new
     SqlConnection(@"Data Source =.\SQLEXPRESS;Initial Catalog = DBTest1;Integrated Security= True"
    );

    But, the user doesn't have access to SQL Server as a Login. Please try following these steps to add that user as SQL Server Login:

    1. Open Microsoft SQL Server Management Studio
    2. Go to Security Section
    3. Right-Click on Logins , then click on New Login...
    4. On the "Login - New" window, please click the "Search..." button
    5. On the "Select User or Group" window you should enter your user name, if you don't know it completely write some text of the name and then click Check Names , after your find the user "Bahador-gh-PC\Bahador-gh" or another one, please click OK.
    6. Check "Windows Authentication" Radio Button
    7. On the left panel, click Server Roles select sysadmin
    8. On the left panel, click User Mapping , check the Box on the left side of your Database (DBTest1), that will create an user on that Database for your new Login.


    After doing that, please try connecting to SQL Server again from your C# Application.


    Hope this helps.


    Willy Taveras.-

    http://itlearn.net

    Sunday, November 14, 2010 3:13 PM
  • no one can't answer me?

    plz/...... i need it seriously :((


    As others said you don't have access permission to the database.

    Can you tell us how you create this database, with which login to SQL Server? If you are use sa to create the database, you can log in to SQL Server using sa account and add your current Wiindows account to SQL Server as a login, map this login to a database user in your database and grant proper permission. You can refer to the steps provided by Willy above.

    Thanks,
    Chunsong


    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, November 16, 2010 6:26 AM