【问题标题】:error: The connection was not closed, The connection's current state is open [closed]错误:连接没有关闭,连接的当前状态是打开[关闭]
【发布时间】:2014-12-22 03:45:02
【问题描述】:

我不知道我的错误在哪里.. 我已经把 connection.close() 到处。我在 microsoft visual studio 2013 C# 中使用 access 数据库。它在没有数据库的情况下运行良好,但是当我尝试向其中添加数据库时,问题就开始出现了。

private void btnLogin_Click(object sender, EventArgs e)
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "SELECT * from Login where [Username]='" + txtUser.Text + "' and [Password]='" + txtPass.Text + "' ";
        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;
        while (reader.Read())
        { 
            count = count + 1;
        }
        if (count == 1)
        {
            connection.Open();
            Form main = new AdminMain();
            main.Show();
            this.Hide();

            try
            {
                connection.Open();
                command.Connection = connection;
                command.CommandText = "INSERT into LogHisto ([Username],[LogDate],[LogTime]) values ('" + txtUser.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "')";
                command.ExecuteNonQuery();
                connection.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
            connection.Close();
        }

        else
        {
            MessageBox.Show("Username/Password is incorrect");
            try
            {
                connection.Open();
                command.Connection = connection;
                command.CommandText = "INSERT into LogHisto (Username,LogDate,LogTime) values ('" + txtUser.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "')";
                command.ExecuteNonQuery();
                connection.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
        }
        connection.Close();
    }

【问题讨论】:

  • “这就是问题开始出现的地方。” - 什么问题?另外:使用参数化查询。
  • 我试过了,但还是不行。我只是试图让我的表单将登录名和日期保存到数据库中,但是因为每当登录成功或没有我的代码混淆时,我都会打开和关闭它与数据库之间的连接。如果我不尝试保存登录名,它工作得很好但是当我尝试将它保存到我的数据库中的一个表中时,我遇到了死胡同。

标签: c# ms-access


【解决方案1】:
    if (count == 1)
    {
        connection.Open(); // <-------------FIRST TIME
        Form main = new AdminMain();
        main.Show();
        this.Hide();

        try
        {
            connection.Open();// <-------------SECOND TIME
            command.Connection = connection;
            command.CommandText = "INSERT into LogHisto ([Username],[LogDate],[LogTime]) values ('" + txtUser.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "')";
            command.ExecuteNonQuery();
            connection.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }
        connection.Close();
    }

在此代码块中,您打开相同的连接两次,因为您有此异常。只需删除connection.Open() 之一;您还需要删除connection.Close(); 之一。

您还应该在查询中使用参数!这将防止sql注入。我建议您为DataAccess 制作不同的课程。这将防止这样的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多