ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

今天调试一个监控程序,放到环境里发现不写库,于是给SQL执行类异常事件做了个日志,发现报:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

左看右看,没问题呀,后来才想起,using里没有执行db.Open,打开数据库连接。

 

    public class SQLHelper : IDisposable
    {
        private IntPtr handle;
        private readonly Component component = new Component();
        private bool disposed;
        private readonly string _connstr = Config.ConnectionString;

        #region 执行SQL语句,返回影响行数
        /// 
        /// 执行查询,返回影响行数
        /// 
        /// 
        /// 
        public int iExecute(string sql)
        {
            int intTemp;
            try
            {
                using (var db = new SqlConnection(_connstr))
                {
                    db.Open();
                    var cmd = new SqlCommand(sql, db) {CommandType = CommandType.Text};
                    intTemp = cmd.ExecuteNonQuery();
                    db.Close();
                }
            }
            catch(Exception ex)
            {
                FileHelper.AppendFile("D:\\Log.txt", string.Format("{0}[{1}][{2}]",sql ,ex.Message,_connstr));
                intTemp = 0;
            }
            return intTemp;

        }
        #endregion

        #region 执行SQL语句,返回表结果
        /// 
        /// 查询返回DataSet
        /// 
        /// 
        /// 
        public DataSet pExecute(string sql)
        {
            var ds = new DataSet();
            try
            {
                using (var db = new SqlConnection(_connstr))
                {
                    db.Open();
                    var dr = new SqlDataAdapter();
                    var cmd = new SqlCommand(sql, db) {CommandType = CommandType.Text};
                    dr.SelectCommand = cmd;
                    dr.Fill(ds);
                    ds = (ds.Tables.Count == 0) ? null : ds;
                    db.Close();
                }
            }
            catch{}
            return ds;
        }
        #endregion

        #region 执行存储过程,返回查询的表结果
        /// 
        /// 执行存储过程,返回查询的表结果
        /// 
        /// 
        /// 
        /// 
        public DataSet pExecute(string produceName, SqlParameter[] param)
        {
            var ds = new DataSet();
            try
            {
                using (var db = new SqlConnection(_connstr))
                {
                    db.Open();
                    var cmd = new SqlCommand {Connection = db,CommandType = CommandType.StoredProcedure,CommandText = produceName};
                    var dr = new SqlDataAdapter();
                    foreach (var t in param)
                    {
                        cmd.Parameters.Add(t);
                    }
                    dr.SelectCommand = cmd;
                    dr.Fill(ds);
                    db.Close();
                }
            }
            catch (Exception){}
            return ds;
            
        }
        #endregion

        #region 执行存储过程,返回影响行数
        /// 
        /// 执行存储过程,返回影响行数
        /// 
        /// 
        /// 
        /// 
        public int iExecute(string produceName, SqlParameter[] param)
        {
            int intTemp = 0;
            try
            {
                using (var db = new SqlConnection(_connstr))
                {
                    db.Open();
                    var cmd = new SqlCommand
                                  {
                                      Connection = db,
                                      CommandType = CommandType.StoredProcedure,
                                      CommandText = produceName
                                  };
                    foreach (var t in param)
                    {
                        cmd.Parameters.Add(t);
                    }
                    intTemp = cmd.ExecuteNonQuery();
                    db.Close();
                }
            }
            catch (Exception){}
            return intTemp;
        }
        #endregion

        #region Dispose实现
        public void Dispose()
        {
            try
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            catch (Exception)
            {
            }

        }
        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    component.Dispose();
                }
                CloseHandle(handle);
                handle = IntPtr.Zero;
                disposed = true;
            }
        }
        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);
        #endregion


    }

 

 

 

转载于:https://www.cnblogs.com/blackice/archive/2012/08/17/2644901.html

你可能感兴趣的:(ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.)