关于System.InvalidOperationException: 句柄未初始化的问题

时间:2022-03-08 03:42:15
有源代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 外卖管理软件
{
    class Connection
    {
        private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + System.IO.Directory.GetCurrentDirectory() + "\\database.mdb";//设置连接字符
        private OleDbConnection connection=null; //连接对象
        private OleDbCommand command=null; //命令对象
        private OleDbDataAdapter dataAdapter=null;//数据适配器对象
        private DataSet ds=null; //数据集

        public Connection() {
            SetConnection();
        }

        ~Connection() {
            closeConnection();
        }

        private void SetConnection(){
            connection = new OleDbConnection(connectionString);//创建连接对象
            connection.Open();//打开连接
            command = new OleDbCommand();//创建Command对象
            command.Connection = connection;//通过connection连接对象操作数据库
            command.CommandType = CommandType.Text;//设置命令类型
        }

        public  bool executeOption(string sql)
        {
            command.CommandText = sql;//设置要执行的命令
            try
            {
                command.ExecuteNonQuery();//执行SQL命令
                MessageBox.Show("操作成功!", "提示");
                return true;
            }
            catch (InvalidOperationException) 
            {
                MessageBox.Show("操作失败!", "警告");
                return false;
            }
        }

        public DataTable getResult(string sql) 
        {
            command.CommandText = sql;//设置要执行的命令
            try
            {
                command.ExecuteNonQuery();//执行SQL命令
                dataAdapter = new OleDbDataAdapter();//生成OleDbDataAdapter对象
                dataAdapter.SelectCommand = command;//设置dataAdapter对象的SelectCommand属性值为创建的OleDbCommand对象名
                ds = new DataSet();//产生新的数据集
                dataAdapter.Fill(ds,"curList");
                DataTable dt = ds.Tables["curList"];
                return dt;
            }
            catch (Exception)
            {
                MessageBox.Show("获取数据失败!", "警告");
                return null;
            }
        }

        private void closeConnection() 
        {
            try
            {
                if (this.connection!=null||this.connection.State == ConnectionState.Open)
                {
                    this.connection.Close();
                    this.connection.Dispose();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("断开数据库连接失败!\n原因如下:\n" + e.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}
运行时,总会弹出提示框,提示框内容如下:
断开数据库连接失败!
原因如下:
{System.InvalidOperationException: 句柄未初始化。   
在 System.WeakReference.set_Target(Object value)   
在 System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)   
在 System.Data.OleDb.OleDbConnection.Close()   
在 外卖管理软件.Connection.closeConnection()}

求解,这是为什么?

2 个解决方案

#1


 if (this.connection!=null  && this.connection.State == ConnectionState.Open)
                {
                    this.connection.Close();
                    this.connection.Dispose();
   this.connection=null;
                }

  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

#2


试过了,没用。

#1


 if (this.connection!=null  && this.connection.State == ConnectionState.Open)
                {
                    this.connection.Close();
                    this.connection.Dispose();
   this.connection=null;
                }

  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

#2


试过了,没用。