none
Visual Studio 2017 - SQLServer Connection Error RRS feed

  • Question

  • Hello Everyone, 

    I am trying the code below which results in a connection error. Important notes; I am using a local DB, I can connect using SSMS. I placed **** in the servername and data catalog. I am using VS 2017 and SQLServer 2014.

    private void btnConnect_MouseClick(object sender, MouseEventArgs e)
            {
                string cs = @"Data Source=***********\SQLEXPRESS;Initial Catalog=AA*****;Integrated Security=True";
                using (SqlConnection con = new SqlConnection(cs))
                    try
                    {

                        con.Open();
                        MessageBox.Show("connect with sql server");
                        con.Close();

                    }
                catch (Exception es)
                {
                    MessageBox.Show(es.Message);
                }

    Message Error - Cannot open database "AA*****" requested by the login. The login failed. Login failed for user 'domain\aduser" 

    Any help is much appreciated.

    Thursday, November 30, 2017 7:21 PM

All replies

  • You say you are using LocalDB, then the connectionsting example is in the link.

    http://www.michael-whelan.net/sql-server-localdb-2014-connection-string/

    The one you are trying to use is when you move the MDF file to the MS SQL Express directory and Attach the MDF file to the database engine.

    Thursday, November 30, 2017 9:11 PM
  • Hello CWhyte_gov,

    Your connection string shows that you want to connect SQLEXPRESS rather than LocalDb. And I can't get more detailed information about the login authentication ways. The windows authentication ,SQL Server authentication  Or others? And you could try to create a SQL Server login account for your SQL Server. Your connection string will like below.

    connectionString="Server=.\SQLExpress;Database=database;User ID=userid;pwd=password"
    

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 1, 2017 7:13 AM
    Moderator
  • Hi CWhyte_gov,

    Based on your connection string, it seems that you use a wrong connection string if you want to connect localdb, if you use localdb, I would suggest that you could modify your connection string like this:

     string cs = @"data source=(localdb)\MSSQLLocalDB;initial catalog=YourDatabaseName;integrated security=True";

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Fei HuModerator Wednesday, December 13, 2017 2:54 AM
    Wednesday, December 13, 2017 2:52 AM
    Moderator
  • Hello CWhyte_gov,

    Do you have anything to update? If the issue has been solved, you could mark the helpful reply as answer. This can be beneficial to other community members reading this thread. Thanks a lot.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 24, 2017 6:09 AM
    Moderator