none
Cannot open database. The login failed. RRS feed

  • Question

  • Dear Experts,

    I'm creating a database application. I create a simple dialog with a button. On button click event i'm trying to create a db. But while I'm doing "myConn1.Open();" in below code, I'm getting exception like "Cannot open database. The login failed." But with same user name and password i'm able to open another connection (conn.Open()). Please let me know what is the wrong in the code. 
    I'm newbie and small help is greatly appreciated.


    try            {                SqlConnection myConn = new SqlConnection("Server=SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=master");                SqlConnection myConn1 = new SqlConnection("Server=SI-SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabaseData");                                String str = "CREATE DATABASE MyDatabase ON PRIMARY " +                    "(NAME = MyDatabase_Data, " +                    "FILENAME = 'C:\\MyDatabaseData.mdf', " +                    "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +                    "LOG ON (NAME = MyDatabase_Log, " +                    "FILENAME = 'C:\\MyDatabaseLog.ldf', " +                    "SIZE = 1MB, " +                    "MAXSIZE = 5MB, " +                    "FILEGROWTH = 10%)";                                    SqlCommand myCommand = new SqlCommand(str, myConn);                                    String str1 = "CREATE TABLE myTable" +                                "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +                                "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";                SqlCommand myCommand1 = new SqlCommand(str1, myConn1);                try                 {                    myConn.Open();	                myCommand.ExecuteNonQuery();                    myConn.Close();                    myConn1.Open(); // Exception Occurring Here                    myCommand1.ExecuteNonQuery();                    MessageBox.Show("DataBase is Created Successfully",                                     "MyProgram",                                     MessageBoxButtons.OK,                                     MessageBoxIcon.Information);                }                catch (System.Exception ex)                {                    MessageBox.Show(ex.ToString(),                                     "MyProgram",                                     MessageBoxButtons.OK,                                     MessageBoxIcon.Information);                }                finally                {                    if (myConn.State == ConnectionState.Open)                    {	                    myConn.Close();                    }                }            }            catch (Exception exp)            {                MessageBox.Show("Error\n"+exp);                throw;            }        }

    Wednesday, April 25, 2012 9:02 AM

Answers

  • Hi KS_msdn,

    Welcome to MSDN Forum.

    Because you just create a .mdf file in your c disk, but doesn't attach the .mdf file to SQL Server, so the connection can't be open. Please open your sql server, right click the "database" folder and select ''Attach" option. Then, there will be an adding window appears, you can click the "Add" button to select the database you have created, after that, click "OK" button. you will find the database has already exist in SQL Server. Finally, the connection can be opened now.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 26, 2012 2:29 AM
    Moderator