Sql server 2008 我已经尽力解决了,但我能';T请帮帮我,连接没有关闭。连接';它的当前状态是开放的

Sql server 2008 我已经尽力解决了,但我能';T请帮帮我,连接没有关闭。连接';它的当前状态是开放的,sql-server-2008,c#-4.0,Sql Server 2008,C# 4.0,我正在尝试在我的数据库中插入日期(从和到),T尝试此代码,但它总是显示以下错误: 连接没有关闭。连接的当前状态为“打开” 当我删除中间con.Open()时,它会引发一个异常: ExecuteReader:尚未初始化连接属性 我不明白你在做什么下面的代码 SqlCommand cmd = new SqlCommand(); con.Open(); SqlDataReader r = cmd.ExecuteReader(); cmd将执行什么命令。没有指定任何命令文本 据我所知,您应该创建单独的

我正在尝试在我的数据库中插入日期(从和到),T尝试此代码,但它总是显示以下错误:

连接没有关闭。连接的当前状态为“打开”

当我删除中间con.Open()时,它会引发一个异常:

ExecuteReader:尚未初始化连接属性


我不明白你在做什么下面的代码

SqlCommand cmd = new SqlCommand();
con.Open();
SqlDataReader r = cmd.ExecuteReader();
cmd将执行什么命令。没有指定任何命令文本

据我所知,您应该创建单独的连接类,这有助于正确维护代码

public class AppConnection
{
    public string ConnectionString
    {
        get { return ConfigurationManager.ConnectionStrings["WebConfigConnectionStringName"].ConnectionString; }
    }

    public SqlConnection MakeConnection()
    {
        SqlConnection newConnection = new SqlConnection(this.ConnectionString);
        if (newConnection.State == ConnectionState.Closed)
        {
            newConnection.Open();
        }

        return newConnection;
    }

    public SqlDataReader ExecCommand(string commandText, CommandType commandType, ref SqlConnection appConnection)
    {
        SqlDataReader reader;

        using (SqlCommand dbCommand = new SqlCommand())
        {
            dbCommand.CommandText = commandText;
            dbCommand.CommandType = commandType;
            dbCommand.Connection = appConnection;

            reader = dbCommand.ExecuteReader();
        }

        return reader;
    }
}
按钮点击事件可能如下所示:-

private void btnDiff_Click(object sender, EventArgs e)
    {
        DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
        DateTime fromdate = oldDate;
        DateTime todate = oldDate;

        AppConnection appConnection = new AppConnection();
        SqlConnection con = appConnection.MakeConnection();

        SqlDataReader readerTopics = appConnection.ExecCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", CommandType.Text, ref con);
        while (readerTopics.Read())
        {
            string column = readerTopics["Topic_ID"].ToString();
            int a = Convert.ToInt32(readerTopics["Topic_ID"]);

            fromdate = todate.AddDays(0);
            todate = fromdate.AddDays(7);

            using (SqlCommand updateCommand = new SqlCommand())
            {
                updateCommand.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
                updateCommand.Connection = con;
                updateCommand.CommandType = CommandType.Text;
                updateCommand.ExecuteNonQuery();
            }
        }
    }

我就这么做了&我的问题解决了:)


最好显示
con
变量是如何初始化的关闭前两次。我用SqlDataAdapter完成了。谢谢。您可以在外部上下文中打开连接一次,然后在最后关闭它。但是在循环中,每次迭代都要打开和关闭连接。如果你已经解决了这个问题,那就不行了。给你的问题写一个答案。这样其他人可以从中受益。这里的连接将在哪里关闭?我个人不喜欢从方法返回Reader对象。返回某种类型的DTO对象(如List或DataTable等)可能更好。@Emmad Kareem,我在这里没有提到,但连接关闭方法将在AppConenction类中定义,如果只循环到Reader,则不需要转换为List或DataTable,因为这也需要内存中的空间。那么,如果没有绑定到任何控件,为什么要转换它呢?我的观点是,读取和更新都是一个逻辑过程的一部分,它们不需要拆分为多个方法。此外,应避免直接传递参数。最好使用参数。
private void btnDiff_Click(object sender, EventArgs e)
    {
        DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
        DateTime fromdate = oldDate;
        DateTime todate = oldDate;

        AppConnection appConnection = new AppConnection();
        SqlConnection con = appConnection.MakeConnection();

        SqlDataReader readerTopics = appConnection.ExecCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", CommandType.Text, ref con);
        while (readerTopics.Read())
        {
            string column = readerTopics["Topic_ID"].ToString();
            int a = Convert.ToInt32(readerTopics["Topic_ID"]);

            fromdate = todate.AddDays(0);
            todate = fromdate.AddDays(7);

            using (SqlCommand updateCommand = new SqlCommand())
            {
                updateCommand.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
                updateCommand.Connection = con;
                updateCommand.CommandType = CommandType.Text;
                updateCommand.ExecuteNonQuery();
            }
        }
    }
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
            DateTime fromdate = oldDate;
            DateTime todate = oldDate;

            SqlCommand cmd1 = new SqlCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", con);
            con.Open();

            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
            adapter.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                string column = row["Topic_ID"].ToString();
                int a = Convert.ToInt32(row["Topic_ID"]);

                fromdate = todate.AddDays(0);
                todate = fromdate.AddDays(7);

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
                cmd.ExecuteScalar();
                this.topicTableAdapter.Fill(this.courseDataSet.Topic);