基础数据库对象类

/// <summary>
    /// 基础数据库对象,负责对数据库提供支持
    /// </summary>
    public class SqlDB : System.IDisposable
    {
        private string _connstring;
        protected bool IsCreateNewConnection = false;
        private SqlConnection _conn;
        protected string _tableName;
        /// <summary>
        /// 声明一个新实例
        /// </summary>
        public SqlDB()
        {
            _connstring = SysConfig.ConnectionString("MSSQL_TG");
        }
        /// <summary>
        /// 从 WebConfig.xml 里获取指定名字的 数据连接字符串
        /// </summary>
        /// <param name="ConnectionName"></param>
        public SqlDB(string ConnectionName)
        {
            _connstring = SysConfig.ConnectionString(ConnectionName);
        }
        /// <summary>
        /// 获取sqlConnection连接对象
        /// </summary>
        public SqlConnection conn
        {
            get
            {
                //使用 _create_new_conn 标识 是否创建一个新的连接对象
                //对于 DataReader 对象是一个持久的连接 ,可能与其他操作有冲突,因此需要一个新的连接
                if (IsCreateNewConnection)
                {
                    return new SqlConnection(_connstring);
                }
                if (_conn == null)
                {
                    _conn = new SqlConnection(_connstring);
                }
                return _conn;
            }
        }
        public void Close()
        {
            if (_conn != null)
            {
                _conn.Close();
                _conn.Dispose();
            }
        }

        /// <summary>
        /// 数据表名[预留]
        /// </summary>
        public string tableName
        {
            get
            {
                return _tableName;
            }
            set
            {
                _tableName = value;
            }
        }
        /// <summary>
        /// 执行SQL语句,并且返回影响的行数
        /// 假如返回-1,则表明执行失败,可能是连接数据失败
        /// </summary>
        /// <param name="sql">要执行的SQL语句</param>
        /// <returns></returns>
        public int Execute(string sql)
        {
            try
            {
                return GetCommand(sql, CommandType.Text).ExecuteNonQuery();
            }
            catch
            {
                return -1;
            }
        }
        public int Execute(string cmdText, CommandType cmdType, params SqlParameter[] parms)
        {
            try
            {
                return GetCommand(cmdText, cmdType, parms).ExecuteNonQuery();
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 返回结果页中第一行第一列数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sql)
        {
            return ExecuteScalar(sql, CommandType.Text);
        }
        public object ExecuteScalar(string sql, CommandType commandType, params SqlParameter[] sqlParameter)
        {
            return GetCommand(sql, commandType, sqlParameter).ExecuteScalar();
        }
        /// <summary>
        /// 执行一个Command对象,返回SqlDataReader
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public SqlDataReader GetDataReader(string cmdText, CommandType cmdType, params SqlParameter[] parms)
        {
            IsCreateNewConnection = true;
            SqlCommand cmd = GetCommand(cmdText, cmdType, parms);
            IsCreateNewConnection = false;
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        public DataSet GetDataSet(string cmdText, CommandType cmdType, params SqlParameter[] parms)
        {
            DataSet set = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(GetCommand(cmdText, cmdType, parms));
            sda.Fill(set);
            return set;
        }

        public DataTable GetDataTable(string cmdText, CommandType cmdType, params SqlParameter[] parms)
        {
            DataTable table = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(GetCommand(cmdText, cmdType, parms));
            sda.Fill(table);
            return table;
        }
        /// <summary>
        /// 获取一个 Command 执行对象
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public SqlCommand GetCommand(string cmdText, CommandType cmdType, SqlParameter[] parms)
        {
            SqlConnection sqlconn = this.conn;
            try
            {
                SqlCommand cmd = new SqlCommand(cmdText, sqlconn);
                cmd.CommandType = cmdType;
                for (int i = 0; i < parms.Length; i++)
                {
                    cmd.Parameters.Add(parms[i]);
                }
                if (sqlconn.State != ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                return cmd;
            }
            finally { }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        public SqlCommand GetCommand(string cmdText, CommandType cmdType)
        {
            return GetCommand(cmdText, cmdType, new SqlParameter[0]);
        }
        /// <summary>
        /// 更新DateTable到数据库,[预留]
        /// 暂时未解决如何自动生成SqlDataAdapter的UpdateCommend
        /// </summary>
        /// <param name="dt">要更新的数据表</param>
        /// <returns></returns>
        public int Update(DataTable dt)
        {
            SqlConnection sqlconn = conn;
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("", sqlconn);
                return da.Update(dt);
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 更新DataSet到数据库,暂时未解决如何自动生成SqlDataAdapter的UpdateCommend,因此无法使用
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public int Update(DataSet ds)
        {
            SqlConnection sqlconn = conn;
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("", sqlconn);
                return da.Update(ds);
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 获取一个SqlDataAdapter对象
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public SqlDataAdapter getDataAdapter(string sql)
        {
            return new SqlDataAdapter(sql, this.conn);
        }
        /// <summary>
        /// 获取一个 DataSet 对象
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql)
        {
            return GetDataSet(sql, CommandType.Text);
        }
        /// <summary>
        /// 获取一个DataTable对象
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable getDataTable(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlDataAdapter sda = getDataAdapter(sql);
                if (sda != null)
                {
                    sda.Fill(dt);
                }
                sda.Dispose();
            }
            catch (Exception e)
            {
                throw e;
            }
            return dt;
        }
        /// <summary>
        /// 通过sql语句获取一个 SqlDataReader对象
        /// </summary>
        /// <param name="Sql"></param>
        /// <returns></returns>
        public SqlDataReader getDataReader(string Sql)
        {
            return GetDataReader(Sql, CommandType.Text);
        }
        public DbDataAdapter Adapter(string cmd, bool UseCMDBuilder)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(cmd, conn);
            if (UseCMDBuilder)
            {
                SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            }
            return (DbDataAdapter)adapter;
        }

    }

你可能感兴趣的:(数据库)