简体   繁体   中英

Can't create a database programmatically

I am trying to create a database through visual studios, however I can't. I get the error message: "Cannot open database "database1" requested by the login. the login failed. Login failed for user '123'" However if I manually create "database1" in SQL server studios and then try to connect to it via c# code I can connect to the database fine. I have created a user with the id 123 and password abc in server studios with every server role checkbox ticked. I am using SQL Express 2012.

private void sQLToolStripMenuItem_Click(object sender, EventArgs e)
    {
        bool create_db = false;
        bool create_table = false;


        SqlConnection myConnection = new SqlConnection("Server = localhost; Database = database1; User Id = 123; Password = abc;");
        try
        {

            myConnection.Open();
            MessageBox.Show("We were able to connect to the database!", "Success!");
            create_table = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            create_db = true;
            MessageBox.Show("There is no database. We will create one.", "No Database Found.");
        }

        // For creating the database if not found
        if (create_db == true)
        {
            String str;
            SqlConnection myConn = new SqlConnection("Server = localhost; Database = database1; User Id = 123; Password = abc;");

            str = "CREATE DATABASE database1";

            SqlCommand myCommand = new SqlCommand(str, myConn);
            try
            {
                myConn.Open();
                myCommand.ExecuteNonQuery();
                MessageBox.Show("DataBase is Created Successfully", "Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                create_db = false;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (myConn.State == ConnectionState.Open)
                {
                    myConn.Close();
                }
            }
        }

基于注释:如果您尝试以编程方式创建数据库,则无法使用与未出生数据库的连接,因此在这种情况下,您必须打开与master数据库的连接,如下所示:

"Server = localhost; Database = master; User Id = 123; Password = abc;"
        String str;
        SqlConnection myConn = new SqlConnection("Server=localhost;User Id = 123; Password = abc;database=master");

        str = "CREATE DATABASE database1";

        SqlCommand myCommand = new SqlCommand(str, myConn);
        try
        {
            myConn.Open();
            myCommand.ExecuteNonQuery();
            MessageBox.Show("DataBase is Created Successfully", "Creation", 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();
            }
        }

Please try this above code. Its working for me.Don't give database name before you create the database. Use database master for initialize the database connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM