Ejemplo n.º 1
0
        /// <summary>
        /// 创建一个新的数据库连接对象
        /// </summary>
        /// <param name="driveType">数据库类型</param>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns></returns>
        public static DbConnection CreateDbConnection(this StDbDriveType driveType, string connectionString)
        {
            DbConnection conn = null;

            switch (driveType)
            {
            case StDbDriveType.ODBC:
                conn = new OdbcConnection(connectionString);
                break;

            case StDbDriveType.OleDB:
                conn = new OleDbConnection(connectionString);
                break;

            case StDbDriveType.Oracle:
                conn = new OracleConnection(connectionString);
                break;

            case StDbDriveType.SQLServer:
                conn = new SqlConnection(connectionString);
                break;

            case StDbDriveType.MySQL:
                conn = new MySqlConnection(connectionString);
                break;

            case StDbDriveType.SQLite:
                conn = new SQLiteConnection(connectionString);
                break;
            }
            return(conn);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建一个DbCommand的参数项
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="parName">要创建的对象的名称 如:@pageIndex、@id等</param>
        /// <param name="obj">参数的值</param>
        public static DbParameter CreateDbParameter(this StDbDriveType driveType, string parName, object obj)
        {
            DbParameter par = null;

            switch (driveType)
            {
            case StDbDriveType.ODBC:
                par = new OdbcParameter(parName, obj);
                break;

            case StDbDriveType.OleDB:
                par = new OleDbParameter(parName, obj);
                break;

            case StDbDriveType.Oracle:
                par = new OracleParameter(parName, obj);
                break;

            case StDbDriveType.SQLServer:
                par = new SqlParameter();
                break;

            case StDbDriveType.MySQL:
                par = new MySqlParameter(parName, obj);
                break;

            case StDbDriveType.SQLite:
                par = new SQLiteParameter(parName, obj);
                break;

            default:
                break;
            }
            return(par);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 用指定参数实例化一个数据库连接
 /// </summary>
 /// <param name="dbDriveType">数据库驱动类型</param>
 /// <param name="dbType">数据库类型</param>
 /// <param name="connectionString">数据库连接字符串</param>
 private void Init(StDbDriveType dbDriveType, StDbType dbType, string connectionString)
 {
     //初始化数据库驱动类型
     _dbDriveType = dbDriveType;
     //初始化数据库类型
     _dbType = dbType;
     //数据库连接字符串
     _connectionString = connectionString;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 创建一个DbCommand的参数项
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="parName">要创建的对象的名称 如:@pageIndex、@id等</param>
        /// <param name="type">数据库中的类型</param>
        /// <param name="val">要创建的对象的值  为空表示不设置</param>
        /// <param name="size">要创建的对象的最大大小  小于或等于0表示不设置 </param>
        /// <returns></returns>
        public static DbParameter CreateDbParameter(this StDbDriveType driveType, string parName, DbType type, int size)
        {
            DbParameter par = driveType.CreateDbParameter(parName, type);

            if (size != 0)
            {
                par.Size = size;
            }

            return(par);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlCommand</returns>
        public static DbCommand CreateDbCommand(this StDbDriveType driveType, DbConnection connection, string storedProcName, DbParameter[] parameters)
        {
            //DbParameter
            DbCommand comm = driveType.CreateDbCommand(connection, storedProcName, CommandType.StoredProcedure);

            if (parameters != null && parameters.Length > 0)
            {
                comm.Parameters.AddRange(parameters);
            }
            return(comm);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 创建一个新的DBCommand
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="connString">数据库连接对象</param>
        /// <param name="text">执行的命令文本</param>
        /// <param name="type">执行方式</param>
        /// <param name="tran">一个事务  此项可为null</param>
        /// <param name="pars">参数数组,如果没有可为null</param>
        /// <returns></returns>
        public static DbCommand CreateDbCommand(this StDbDriveType driveType, DbConnection conn, string text, CommandType type, DbTransaction tran, params DbParameter[] pars)
        {
            DbCommand cmd = null;

            switch (driveType)
            {
            case StDbDriveType.ODBC:
                cmd = new OdbcCommand();
                break;

            case StDbDriveType.OleDB:
                cmd = new OleDbCommand();
                break;

            case StDbDriveType.Oracle:
                cmd = new OracleCommand();
                break;

            case StDbDriveType.SQLServer:
                cmd = new SqlCommand();
                break;

            case StDbDriveType.MySQL:
                cmd = new MySqlCommand();
                break;

            case StDbDriveType.SQLite:
                cmd = new SQLiteCommand();
                break;

            default:
                break;
            }
            cmd.CommandType = type;
            cmd.Connection  = conn;
            if (tran != null)
            {
                cmd.Transaction = tran;
            }
            cmd.CommandText = text;
            if (pars != null && pars.Length > 0)
            {
                cmd.Parameters.AddRange(pars);
            }
            return(cmd);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 创建一个DbCommand的参数项
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="parName">要创建的对象的名称 如:@pageIndex、@id等</param>
        /// <param name="type">数据库中的类型</param>
        /// <param name="val">要创建的对象的值  为空表示不设置</param>
        /// <param name="size">要创建的对象的最大大小  小于或等于0表示不设置 </param>
        /// <returns></returns>
        public static DbParameter CreateDbParameter(this StDbDriveType driveType, string parName, DbType type, int size, object val)
        {
            DbParameter par = driveType.CreateDbParameter(parName, type);

            if (size != 0)
            {
                par.Size = size;
            }

            if (val != null)
            {
                par.Value = val;
            }
            else
            {
                par.Value = DBNull.Value;
            }
            return(par);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 创建一个DbCommand的参数项
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="parName">要创建的对象的名称 如:@pageIndex、@id等</param>
        /// <param name="type">数据库中的类型</param>
        public static DbParameter CreateDbParameter(this StDbDriveType driveType, string parName, DbType type)
        {
            DbParameter par = null;

            switch (driveType)
            {
            case StDbDriveType.ODBC:
                par = new OdbcParameter();
                break;

            case StDbDriveType.OleDB:
                par = new OleDbParameter();
                break;

            case StDbDriveType.Oracle:
                par = new OracleParameter();
                break;

            case StDbDriveType.SQLServer:
                par = new SqlParameter();
                break;

            case StDbDriveType.MySQL:
                par = new MySqlParameter();
                break;

            case StDbDriveType.SQLite:
                par = new SQLiteParameter();
                break;

            default:
                break;
            }
            par.ParameterName = parName;
            if (type != default(DbType))
            {
                par.DbType = type;
            }
            return(par);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 创建一个DbCommand的参数项
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="parName">要创建的对象的名称 如:@pageIndex、@id等</param>
        /// <param name="val">参数的值</param>
        public static DbParameter CreateDbParameter(this StDbDriveType driveType, string parName, DbType dbType, object val)
        {
            DbParameter par = null;

            switch (driveType)
            {
            case StDbDriveType.ODBC:
                par = new OdbcParameter();

                break;

            case StDbDriveType.OleDB:
                par = new OleDbParameter();
                break;

            case StDbDriveType.Oracle:
                par = new OracleParameter();
                break;

            case StDbDriveType.SQLServer:
                par = new SqlParameter();
                break;

            case StDbDriveType.MySQL:
                par = new MySqlParameter();
                break;

            case StDbDriveType.SQLite:
                par = new SQLiteParameter();
                break;

            default:
                break;
            }
            par.ParameterName = parName;
            par.Value         = val;
            par.DbType        = (DbType)dbType;
            return(par);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 创建一个DbCommand的参数项
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <param name="parName">要创建的对象的名称 如:@pageIndex、@id等</param>
        /// <param name="type">数据库中的类型</param>
        /// <param name="val">要创建的对象的值  为空表示不设置</param>
        /// <param name="size">要创建的对象的最大大小  小于或等于0表示不设置 </param>
        /// <param name="souceSourceColumnName">该对象映射到数据集中的列明</param>
        /// <returns></returns>
        public static DbParameter CreateDbParameter(this StDbDriveType driveType, string parName, DbType type, int size, object val, string souceSourceColumnName)
        {
            DbParameter par = driveType.CreateDbParameter(parName, type);

            if (size > 0)
            {
                par.Size = size;
            }

            if (val != null)
            {
                par.Value = val;
            }
            else
            {
                par.Value = DBNull.Value;
            }
            if (string.IsNullOrWhiteSpace(souceSourceColumnName))
            {
                par.SourceColumn = souceSourceColumnName;
            }
            return(par);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 创建一个新的DataAdapter
        /// </summary>
        /// <param name="driveType">数据库驱动类型</param>
        /// <returns></returns>
        public static DbDataAdapter CreateDbDataAdapter(this StDbDriveType driveType, DbConnection conn)
        {
            DbDataAdapter da = null;

            switch (driveType)
            {
            case StDbDriveType.ODBC:
                da = new OdbcDataAdapter();
                break;

            case StDbDriveType.OleDB:
                da = new OleDbDataAdapter();
                break;

            case StDbDriveType.Oracle:
                da = new OracleDataAdapter();
                break;

            case StDbDriveType.SQLServer:
                da = new SqlDataAdapter();
                break;

            case StDbDriveType.MySQL:
                da = new MySqlDataAdapter();
                break;

            case StDbDriveType.SQLite:
                da = new SQLiteDataAdapter();
                break;

            default:
                break;
            }

            return(da);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 创建一个新的数据库连接对象
 /// </summary>
 /// <param name="driveType">数据库类型</param>
 /// <returns></returns>
 public static DbConnection CreateDbConnection(this StDbDriveType driveType)
 {
     return(driveType.CreateDbConnection(string.Empty));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// 用指定参数实例化一个数据库连接
 /// </summary>
 /// <param name="dbDriveType">数据库驱动类型</param>
 /// <param name="dbType">数据库类型</param>
 /// <param name="connectionString">数据库连接字符串</param>
 public DbHelper(StDbDriveType dbDriveType, StDbType dbType, string connectionString)
 {
     Init(dbDriveType, dbType, connectionString);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// 创建一个新的DBCommand
 /// </summary>
 /// <param name="driveType">数据库驱动类型</param>
 /// <returns></returns>
 public static DbCommand CreateDbCommand(this StDbDriveType driveType)
 {
     return(driveType.CreateDbCommand(string.Empty, string.Empty, CommandType.Text, null));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// 创建一个新的DBCommand
 /// </summary>
 /// <param name="driveType">数据库驱动类型</param>
 /// <param name="connString">数据库连接对象</param>
 /// <returns></returns>
 public static DbCommand CreateDbCommand(this StDbDriveType driveType, DbConnection conn)
 {
     return(driveType.CreateDbCommand(conn, string.Empty, CommandType.Text, null));
 }
Ejemplo n.º 16
0
 /// <summary>
 /// 创建一个新的DBCommand
 /// </summary>
 /// <param name="driveType">数据库驱动类型</param>
 /// <param name="connString">数据库连接对象</param>
 /// <param name="text">执行的命令文本</param>
 /// <param name="type">执行方式</param>
 /// <returns></returns>
 public static DbCommand CreateDbCommand(this StDbDriveType driveType, DbConnection conn, string text, CommandType type)
 {
     return(driveType.CreateDbCommand(conn, text, type, null));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// 创建一个新的DBCommand
 /// </summary>
 /// <param name="driveType">数据库驱动类型</param>
 /// <param name="connString">数据库连接字符串</param>
 /// <param name="type">执行方式</param>
 /// <returns></returns>
 public static DbCommand CreateDbCommand(this StDbDriveType driveType, string connString, CommandType type)
 {
     return(driveType.CreateDbCommand(connString, string.Empty, type, null));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// 获取DBCommand
 /// </summary>
 /// <param name="driveType">数据库驱动类型</param>
 /// <param name="connString">数据库连接字符串</param>
 /// <param name="text">执行的命令文本</param>
 /// <returns></returns>
 public static DbCommand CreateDbCommand(this StDbDriveType driveType, string connString, string text)
 {
     return(driveType.CreateDbCommand(connString, text, CommandType.Text, null));
 }