【问题标题】:Can't create a database programmatically无法以编程方式创建数据库
【发布时间】:2016-03-27 04:24:34
【问题描述】:

我正在尝试通过 Visual Studio 创建数据库,但我做不到。我收到错误消息:“无法打开登录请求的数据库“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();
                }
            }
        }

【问题讨论】:

  • 您不能指定一个不存在的数据库。如果您需要创建数据库,请不要指定 initial catalogdatabase 值。成功创建数据库后,就可以指定数据库了。
  • 我是 SQL 新手,是否像删除“Database = database1;”一样简单从连接字符串?我不太明白“成功创建数据库后我可以指定它。”
  • 亲爱的@Vince,请考虑使用 master 数据库作为您的初始目录,如下所示:Database = master;
  • mostafa8026 谢谢。我在这上面花了 2 个小时,就这么简单......
  • 发生是因为您从未调试过:很明显,您的错误不是创建数据库,因为错误发生在 myConn.Open () 上 - 在提交命令之前。因此,您的标题是错误的。如果您一直使用调试器一次,您应该已经看到错误发生的位置,然后您就会开始阅读清楚说明的错误。

标签: c# sql-server visual-studio-2012


【解决方案1】:

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

"Server = localhost; Database = master; User Id = 123; Password = abc;"

【讨论】:

    【解决方案2】:
            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();
                }
            }
    

    请试试上面的代码。它对我有用。在创建数据库之前不要给出数据库名称。使用数据库主数据库初始化数据库连接。

    【讨论】: