• 根据抽象工厂实现的DBHelpers类


    public abstract class DBHelper
        {
            public static SqlConnection conn = new SqlConnection("server=longdabing;database=test;uid=sa;pwd=sasa");
            public DBHelper()
            {
                conn.Open();
            }
            /// <summary>
            /// insert,update,delete
            /// </summary>
            /// <returns></returns>
            public abstract int ExecuteNonQuery(string sql);
            /// <summary>
            /// mulinsert
            /// </summary>
            /// <param name="insertdata"></param>
            /// <returns></returns>
            public abstract bool MutiInsertData(DataTable insertdata);
    
            /// <summary>
            /// 查询select
            /// </summary>
            /// <returns></returns>
            public abstract DataTable ExecuteGetData(string sql);
    
            //public abstract IQueryable<ldbEFEntities> ExecuteEFQuery();
    
            /// <summary>
            /// 调用存储过程
            /// </summary>
            /// <param name="procedurename"></param>
            /// <returns>返回数据集</returns>
            public abstract DataSet ExeStoredProcedure(string procedurename,SqlParameter[] sqlpara);
            /// <summary>
            /// 执行存储过程
            /// </summary>
            /// <param name="procedurename">存储过程名字</param>
            /// <param name="sqlpara">参数</param>
            /// <returns>返回被影响的行数</returns>
            public abstract void ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara,out int rowcount);
            
            }
    
        /// <summary>
        /// sql数据库查询
        /// </summary>
        public class DBHelperSql : DBHelper
        {
            public override int ExecuteNonQuery(string sql)
            {
                int cnt = 0;
                try
                {
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cnt = cmd.ExecuteNonQuery();
                    cmd.Dispose();
    
                    return cnt;
                }
                catch (Exception)
                {
                    return cnt;
                }
                finally
                {
                    conn.Close();
                }
            }
    
            public override DataTable ExecuteGetData(string sql)
            {
                DataTable dt = new DataTable();
                try
                {
                    SqlDataAdapter sdd = new SqlDataAdapter(sql, conn);
                    sdd.Fill(dt);
                }
                catch (Exception)
                {
                    //return dt;
                }
                finally
                {
                    conn.Close();
                }
                return dt;
            }
            /// <summary>
            /// 执行存储过程
            /// </summary>
            /// <param name="procedurename"></param>
            /// <param name="sqlpara"></param>
            /// <returns>返回数据集</returns>
            public override DataSet ExeStoredProcedure(string procedurename,SqlParameter[] sqlpara)
            {
                DataSet ds = new DataSet();
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = procedurename;
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (sqlpara.Length > 0)
                    {
                        cmd.Parameters.AddRange(sqlpara);
                    }
                    //cmd.Parameters.Add("@para", SqlDbType.VarChar, 50);
                    SqlDataAdapter sdd = new SqlDataAdapter(cmd);
                    sdd.Fill(ds);
                }
                catch (Exception)
                {
                    //return dt;
                }
                finally
                {
                    conn.Close();
                }
                return ds;
            }
    
            public override void ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara,out int rowcount)
            {
                rowcount = 0;
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = procedurename;
                    cmd.CommandType = CommandType.StoredProcedure;
                    //SqlParameter para = new SqlParameter();
                    //para.TypeName = "";
                    if (sqlpara.Length > 0)
                    {
                        cmd.Parameters.AddRange(sqlpara);
                    }
                   rowcount = cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    //return dt;
                }
                finally
                {
                    conn.Close();
                }
            }
    
            public override bool MutiInsertData(DataTable insertdata)
            {
                SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
                SqlTransaction sqltran = conn.BeginTransaction();//开启事务
                bulkCopy.DestinationTableName = "inforsqlbulk";
                bulkCopy.BatchSize = insertdata.Rows.Count;
    
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
    
                //for (int i = 0; i < dt.Columns.Count; i++)
                //{
                //    bulkCopy.ColumnMappings.Add(i, i);  //映射定义数据源中的列和目标表中的列之间的关系
                //}
                try
                {
                    if (insertdata != null && insertdata.Rows.Count > 0)
                    {
                        bulkCopy.WriteToServer(insertdata);
                       
                    }
                    return true;
                }
                catch
                {
                    sqltran.Rollback();//回滚。
                    return false;
                }
            }
        }
    
        /// <summary>
        /// EntityFramework 查询
        /// </summary>
        public class DBHelperEF : DBHelper
        {
            public override int ExecuteNonQuery(string sql)
            {
                throw new NotImplementedException();
            }
            public override DataTable ExecuteGetData(string sql)
            {
                throw new NotImplementedException();
            }
    
            public override DataSet ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara)
            {
                throw new NotImplementedException();
            }
    
            public override void ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara, out int rowcount)
            {
                throw new NotImplementedException();
            }
    
            public override bool MutiInsertData(DataTable insertdata)
            {
                throw new NotImplementedException();
            }
        }
        /// <summary>
        /// Access数据库
        /// </summary>
        public class DBHelperAccess : DBHelper
        {
            public override int ExecuteNonQuery(string sql)
            {
                throw new NotImplementedException();
            }
            public override DataTable ExecuteGetData(string sql)
            {
                throw new NotImplementedException();
            }
    
            public override DataSet ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara)
            {
                throw new NotImplementedException();
            }
    
            public override void ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara, out int rowcount)
            {
                throw new NotImplementedException();
            }
    
            public override bool MutiInsertData(DataTable insertdata)
            {
                throw new NotImplementedException();
            }
        }
    
        /// <summary>
        /// Oracle数据库
        /// </summary>
        public class DBHelperOracle : DBHelper
        {
            public override int ExecuteNonQuery(string sql)
            {
                throw new NotImplementedException();
            }
            public override DataTable ExecuteGetData(string sql)
            {
                throw new NotImplementedException();
            }
    
            public override DataSet ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara)
            {
                throw new NotImplementedException();
            }
    
            public override void ExeStoredProcedure(string procedurename, SqlParameter[] sqlpara, out int rowcount)
            {
                throw new NotImplementedException();
            }
    
            public override bool MutiInsertData(DataTable insertdata)
            {
                throw new NotImplementedException();
            }
        }
    
        public abstract class DBHelperFactory
        {
           public abstract DBHelper CreateDBHelper();
        }
    
        public  class DBHelperSQLFactory : DBHelperFactory
        {
            public override DBHelper CreateDBHelper()
            {
                return new DBHelperSql();
            }
        }
    
        public class DBHelperAccessFactory : DBHelperFactory
        {
            public override DBHelper CreateDBHelper()
            {
                return new DBHelperAccess();
            }
        }
    
        public class DBHelperEFFactory : DBHelperFactory
        {
            public override DBHelper CreateDBHelper()
            {
                return new DBHelperEF();
            }
        }
    
        public class DBHelperOracleFactory : DBHelperFactory
        {
            public override DBHelper CreateDBHelper()
            {
                return new DBHelperOracle();
            }
        }
  • 相关阅读:
    通达信自动交易软件 z
    keybd_event跟SendMessage,PostMessage模拟键盘消息的区别 z
    55人班37人进清华北大的金牌教师之32条教育建言! z
    SafeHandle和Dispose z
    Path类型的扩展方法 z
    BinaryReader和BinaryWriter的leaveOpen参数 z
    SQL locate()函数
    MySQL 的instr函数
    新建实体的字段不能够是数据库的关键字
    JS isNaN()函数
  • 原文地址:https://www.cnblogs.com/longdb/p/7747436.html
Copyright © 2020-2023  润新知