SqlCommand之间的连接不紧密

KMC*_*KMC 0 c# wpf sql-server-2016

我以编程方式创建数据库,然后在数据库中创建一个表.创建数据库,但不创建表.

// Create Database
try
{
    using (SqlCommand cmd = new SqlCommand(connstr, sqlConn))
    {
        try
        {
            sqlConn.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        finally
        {
            if (sqlConn.State == ConnectionState.Open)
            {
                sqlConn.Close();
            }
        }
    }
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}

// Create table
try
{
    using (SqlCommand cmd = new SqlCommand(
                    "CREATE TABLE dbo.MyTable ("
                    + "ID int IDENTITY(1,1) PRIMARY KEY,"
                    + "MyProduct nvarchar(100) NOT NULL,"
                    + "MyDateTime datetime NOT NULL);"
                    + "", sqlConn))
    {
        sqlConn.Open();
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
    }
}
catch (Exception ex)
{

    MessageBox.Show(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)

但是我收到错误消息,说我的连接没有关闭.

在此输入图像描述

我尝试重复添加sqlConn.Close()到每个{block}的结尾但仍然得到相同的错误.如何在创建数据库后正确关闭连接,然后再次重新打开连接以创建表?

[编辑]

在输入答案之后,我重新构建了我的代码,我确信包装属性而不重复Open().

    // Create Database
    try
        {
        using (SqlConnection sqlConn = new SqlConnection(sqlConnectionStr))
        {
            using (SqlCommand cmd = new SqlCommand(connstr, sqlConn))
            {
                try
                {
                    sqlConn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                finally
                {
                    if (sqlConn.State == ConnectionState.Open)
                    {
                        sqlConn.Close();
                    }
                }
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show("(1)\n" + ex.ToString());
    }

// Create Table
    try
    {
        using (SqlConnection sqlConn = new SqlConnection(sqlConnectionStr))
        {

            using (SqlCommand cmd = new SqlCommand("CREATE TABLE dbo.MyTable ("
                            + "ID int IDENTITY(1,1) PRIMARY KEY,"
                            + "MyProduct nvarchar(100) NOT NULL,"
                            + "MyDateTime datetime NOT NULL);"
                            + "", sqlConn))
            {
                sqlConn.Open();
                cmd.ExecuteNonQuery();
                sqlConn.Close();
            }
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show("(2)\n" + ex.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

然后我收到错误数据库中已经有一个名为"MyTable"的对象.但是当我调查SSMS时,表格不存在.我现在很困惑.

在此输入图像描述

Gra*_*ICA 5

当您创建表时,您尝试两次打开连接.该cmd.Connection属性指向相同的连接sqlConn.删除其中一个.

using (SqlCommand cmd = new SqlCommand(
                "CREATE TABLE dbo.MyTable ("
                + "ID int IDENTITY(1,1) PRIMARY KEY,"
                + "MyProduct nvarchar(100) NOT NULL,"
                + "MyDateTime datetime NOT NULL);"
                + "", sqlConn))
{
    sqlConn.Open();
    cmd.Connection.Open();  // don't need this...
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();
}
Run Code Online (Sandbox Code Playgroud)

根据文档,这是预期的行为.

出现InvalidOperationException

无法在未指定数据源或服务器的情况下打开连接.

连接已打开.