簡體   English   中英

無法以編程方式創建數據庫

[英]Can't create a database programmatically

我正在嘗試通過視覺工作室創建數據庫,但是我不能。 我收到錯誤消息:“無法打開登錄名請求的數據庫“ database1”。登錄失敗。用戶'123'的登錄失敗”但是,如果我在SQL Server Studio中手動創建“ database1”,然后嘗試通過它連接C#代碼我可以連接到數據庫很好。 我在服務器工作室中創建了一個ID為123,密碼為abc的用戶,並選中了每個服務器角色復選框。 我正在使用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();
            }
        }

請嘗試上面的代碼。 它對我有用。創建數據庫之前,請不要提供數據庫名稱。 使用數據庫主數據庫初始化數據庫連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM