C# 数据库操作工厂模式类

View Code 

namespace DbRealize
{
    #region 数据库访问接口
    /// <summary>
    /// 名称: IDbOperate
    /// 功能: 数据库访问接口
    /// 作者: 
    /// 版本: V1.0
    /// 时间: 2011年09月19日
    /// 修改人:
    /// 修改时间:
    /// 修改描述:
    /// </summary>
    public interface IDbOperate
    {
        #region 执行sql字符串返回数据读取器
        /// <summary>
        /// 执行sql字符串返回数据读取器(DataReader,适合于执行Select T_SQL语句)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据读取器(DataReader)</returns>
        IDataReader ExecuteReader(string sqlText, IDataParameter[] paraList);
        #endregion

        #region 执行sql字符串返回数据记录集(DataSet)
        /// <summary>
        /// 执行sql字符串返回数据记录集(DataSet)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据记录集(DataSet)</returns>
        DataSet ExecuteDataSet(string sqlText, IDataParameter[] paraList);
        #endregion

        #region 执行sql字符串返回数据记录集(DataTable)
        /// <summary>
        /// 执行sql字符串返回数据记录集(DataTable)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据记录集(DataTable)</returns>
        DataTable ExecuteDataTable(string sqlText, IDataParameter[] paraList);
        #endregion

        #region 执行sql字符串返回首行首列的值
        /// <summary>
        /// 执行sql字符串返回首行首列的值(适合于执行Select语句中包含聚合函数的T_SQL语句)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>首行首列的值</returns>
        string ExecuteString(string sqlText, IDataParameter[] paraList);
        #endregion

        #region 执行sql字符串返回影响的行数
        /// <summary>
        /// 执行sql字符串返回影响的行数(适合于执行Update、Delete和Insert T_SQL语句)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>影响的行数(失败返回-1)</returns>
        int ExecuteNonQuery(string sqlText, IDataParameter[] paraList);
        #endregion

        #region 执行sql字符串返回数据记录集第一行NameValue集合
        /// <summary>
        /// 执行sql字符串返回数据记录集第一行NameValue集合
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据记录集第一行NameValue集合</returns>
        NameValueCollection ExecuteNameValueCollection(string sqlText, IDataParameter[] paraList);
        #endregion

        #region 事务执行sql命令
        /// <summary>
        /// 事务执行sql命令,只适合执行Update、Delete和Insert T_SQL语句数组
        /// </summary>
        /// <param name="sqlList">Insert T_SQL语句数组</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>是否成功</returns>
        bool TranExecuteNonQuery(List<string> sqlList, List<IDataParameter[]> paraList);
        #endregion

        #region 插入数据
        /// <summary>
        /// 插入数据,返回影响的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="fieldsName">字段名集合</param>
        /// <param name="values">内容集合</param>
        /// <returns>影响记录行数</returns>
        int DataInsert(string tableName, List<string> fieldsName, List<string> values);
        #endregion

        #region 修改数据
        /// <summary>
        /// 修改数据,返回影响的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="fieldsName">字段名集合</param>
        /// <param name="values">内容集合</param>
        /// <param name="conditions">条件集合 不能带有and</param>
        /// <returns>影响记录行数</returns>
        int DataUpdate(string tableName, List<string> fieldsName, List<string> values, List<string> conditions);
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据,返回影响的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="conditions">条件集合 不能带有and</param>
        /// <returns>影响记录行数</returns>
        int DataDelete(string tableName, List<string> conditions);
        #endregion

        #region 获取下一个序号
        /// <summary>
        /// 获取下一个序号
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="idName">字段名(必须为number类型)</param>
        /// <returns>下一个序号</returns>
        int GetNextId(string tableName, string idName);
        #endregion

        #region 获取下一个序号
        int GetNextId(string sequenceName);
        #endregion

        #region 执行sql把blob字段内容写入文件(oracle连接方式)
        /// <summary>
        /// 执行sql把blob字段内容写入文件(oracle连接方式)
        /// </summary>
        /// <param name="sqlText">sql语句(SELECT REPORTTYPETMEPLATE FROM SEPP_T_REPORT_TYPE WHERE REPORTTYPEID='1')</param>
        /// <param name="filePath">文件完整路径(E:\\file.doc)</param>
        void OracleExecuteReaderToFile(string sqlText, string filePath);
        #endregion
    }
    #endregion 

    #region 数据库访问类
    /// <summary>
    /// 名称: DbOperate<DBConnection, DBCommand, DBDataAdapter>
    /// 功能: 数据库访问类(从IDbOperate接口派生出来)
    /// 作者: 
    /// 版本: V1.0
    /// 时间: 2011年09月19日
    /// 修改人:
    /// 修改时间:
    /// 修改描述:
    /// </summary>
    /// <typeparam name="DBConnection">数据库连接数据类型</typeparam>
    /// <typeparam name="DBCommand">数据库命令数据类型</typeparam>
    /// <typeparam name="DBDataAdapter">数据库数据适配器数据类型</typeparam>
    class DbOperate<DBConnection, DBCommand, DBDataAdapter> : IDbOperate
        where DBConnection : System.Data.IDbConnection, new()
        where DBCommand : System.Data.IDbCommand, new()
        where DBDataAdapter : System.Data.IDbDataAdapter, new()
    {
        #region 非IDbOperate 成员
        /// <summary>
        /// 连接字符串
        /// </summary>
        private string strConn = "";
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionString"></param>
        public DbOperate(string connectionString)
        {
            strConn = connectionString;
        }

        #region 创建命令
        private DBCommand CreateCommand(string commandText, CommandType type, IDataParameter[] paraList)
        {
            DBCommand command = default(DBCommand);

            try
            {
                command = new DBCommand();
                command.CommandText = commandText;
                command.CommandType = type;

                if (paraList != null)
                {
                    foreach (IDataParameter para in paraList)
                    {
                        command.Parameters.Add(para);
                    }
                }
            }
            catch (Exception exp)
            {
                throw new Exception("创建数据库操作的命令对象失败!信息:" + exp.Message);
            }
            return command;
        }

        private DBCommand CreateCommand()
        {
            DBCommand command = default(DBCommand);

            try
            {
                command = new DBCommand();
            }
            catch (Exception exp)
            {
                throw new Exception("创建数据库操作的命令对象失败!信息:" + exp.Message);
            }
            return command;
        }
        #endregion

        #region 判断是否是存储过程
        /// <summary>
        /// 判断是否是存储过程
        /// </summary>
        /// <param name="sqlText">sql字符串</param>
        /// <returns></returns>
        private bool isProc(string sqlText)
        {
            //记录SQL语句的开始字符
            string topText = "";

            if (sqlText.Length > 7)
            {
                //取出字符串的前位
                topText = sqlText.Trim().Substring(0, 7).ToUpper();
                // 如果不是存储过程
                if (topText.Equals("UPDATE ") || topText.Equals("INSERT ") ||

                    topText.Equals("DELETE ") || topText.Equals("ALTER T") ||

                    topText.Equals("ALTER ") || topText.Equals("BACKUP ") ||

                    topText.Equals("RESTORE") || topText.Equals("SELECT "))
                {

                    return false;

                }
                else
                {
                    return true;
                }

            }
            else
            {
                return true;
            }
        }
        #endregion
        #endregion

        #region IDbOperate 成员

        #region 执行sql字符串返回数据读取器
        /// <summary>
        /// 执行sql字符串返回数据读取器(DataReader,适合于执行Select T_SQL语句)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据读取器(DataReader)</returns>
        public IDataReader ExecuteReader(string sqlText, IDataParameter[] paraList)
        {
            IDataReader dataReader = null;
            try
            {
                using (DBConnection dbConn = new DBConnection())
                {
                    DBCommand command;
                    if (!isProc(sqlText))
                    {
                        command = CreateCommand(sqlText, CommandType.Text, paraList);
                    }
                    else
                    {
                        command = CreateCommand(sqlText, CommandType.StoredProcedure, paraList);
                    }
                    command.Connection = dbConn;

                    dbConn.ConnectionString = strConn;
                    dbConn.Open();

                    dataReader = command.ExecuteReader();

                    dbConn.Close();
                    dbConn.Dispose();
                    command.Dispose();
                }
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql字符串返回数据读取器失败!信息:" + exp.Message);
            }
            finally
            {
                dataReader.Dispose();
            }

            return dataReader;
        }
        #endregion

        #region 执行sql把blob字段内容写入文件(oracle连接方式)
        /// <summary>
        /// 执行sql把blob字段内容写入文件(oracle连接方式)
        /// </summary>
        /// <param name="sqlText">sql语句(SELECT REPORTTYPETMEPLATE FROM SEPP_T_REPORT_TYPE WHERE REPORTTYPEID='1')</param>
        /// <param name="filePath">文件完整路径(E:\\file.doc)</param>
        public void OracleExecuteReaderToFile(string sqlText,string filePath)
        {
            try
            {
                using (OracleConnection dbConn = new OracleConnection())
                {
                    OracleCommand command = new OracleCommand();
                    command.Connection = dbConn;
                    command.CommandText = sqlText;
                    command.CommandType = CommandType.Text;

                    dbConn.ConnectionString = strConn;
                    dbConn.Open();

                    OracleDataReader dataReader = command.ExecuteReader();
                    if (dataReader.Read() == true)
                    {
                        //读取BLOB字段   
                        OracleLob BLOB = dataReader.GetOracleLob(0);

                        BlobToFile(BLOB, filePath);
                    }

                    dbConn.Close();
                    dbConn.Dispose();
                    command.Dispose();
                }
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql字符串返回数据读取器失败!信息:" + exp.Message);
            }
        }

        ///   <summary>   
        ///   将ORACLE数据库中BLOB这段内容输出到硬盘上生成文件   
        ///   </summary>   
        ///   <param   name= "BLOB "> BLOB字段内容 </param>   
        ///   <param   name= "filePathName "> 要生成的文件名,绝对路径 </param>   
        private void BlobToFile(OracleLob BLOB, string filePathName)
        {
            //将LOGN值转换为INT值,以便定义BYTE[]长度   
            int actual = Convert.ToInt32(BLOB.Length.ToString());
            byte[] buffer = new byte[actual];
            //将BLOB字段内容读取到字节数组里面   
            BLOB.Read(buffer, 0, actual);

            File.Delete(filePathName);
            //打开一个文件,如果没有则创建此文件   
            System.IO.FileStream mys = new FileStream(filePathName, System.IO.FileMode.OpenOrCreate);
            //将读取到的内容写入到此文件中   
            mys.Write(buffer, 0, buffer.Length);
            mys.Flush();
            //关闭此文件对象   
            mys.Close();
            mys.Dispose();
        }  
        #endregion

        #region 执行sql字符串返回数据记录集(DataSet)
        /// <summary>
        /// 执行sql字符串返回数据记录集(DataSet)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据记录集(DataSet)</returns>
        public DataSet ExecuteDataSet(string sqlText, IDataParameter[] paraList)
        {
            DataSet ds = new DataSet();

            try
            {
                using (DBConnection dbConn = new DBConnection())
                {
                    DBCommand command;
                    if (!isProc(sqlText))
                    {
                        command = CreateCommand(sqlText, CommandType.Text, paraList);
                    }
                    else
                    {
                        command = CreateCommand(sqlText, CommandType.StoredProcedure, paraList);
                    }
                    command.Connection = dbConn;

                    dbConn.ConnectionString = strConn;
                    dbConn.Open();

                    DBDataAdapter da = new DBDataAdapter();
                    da.SelectCommand = command;

                    da.Fill(ds);

                    dbConn.Close();
                    dbConn.Dispose();
                    command.Dispose();
                }
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql字符串返回数据记录集异常!信息:" + exp.Message);
            }
            finally
            {
               
            }

            return ds;
        }
        #endregion

        #region 执行sql字符串返回数据记录集(DataTable)
        /// <summary>
        /// 执行sql字符串返回数据记录集(DataTable)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据记录集(DataTable)</returns>
        public DataTable ExecuteDataTable(string sqlText, IDataParameter[] paraList)
        {
            DataSet ds = new DataSet();

            try
            {
                using (DBConnection dbConn = new DBConnection())
                {
                    DBCommand command;
                    if (!isProc(sqlText))
                    {
                        command = CreateCommand(sqlText, CommandType.Text, paraList);
                    }
                    else
                    {
                        command = CreateCommand(sqlText, CommandType.StoredProcedure, paraList);
                    }
                    command.Connection = dbConn;

                    dbConn.ConnectionString = strConn;
                    dbConn.Open();

                    DBDataAdapter da = new DBDataAdapter();
                    da.SelectCommand = command;

                    da.Fill(ds);

                    dbConn.Close();
                    dbConn.Dispose();
                    command.Dispose();
                }
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql语句异常!\n\n" + sqlText + "\n\n\n信息:" + exp.Message);
            }
            finally
            {
                
            }

            return ds.Tables[0];
        }
        #endregion

        #region 执行sql字符串返回首行首列的值
        /// <summary>
        /// 执行sql字符串返回首行首列的值(适合于执行Select语句中包含聚合函数的T_SQL语句)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>首行首列的值</returns>
        public string ExecuteString(string sqlText, IDataParameter[] paraList)
        {
            object obj = null;
            try
            {
                using (DBConnection dbConn = new DBConnection())
                {
                    DBCommand command;
                    if (!isProc(sqlText))
                    {
                        command = CreateCommand(sqlText, CommandType.Text, paraList);
                    }
                    else
                    {
                        command = CreateCommand(sqlText, CommandType.StoredProcedure, paraList);
                    }
                    command.Connection = dbConn;

                    dbConn.ConnectionString = strConn;
                    dbConn.Open();

                    obj = command.ExecuteScalar();

                    dbConn.Close();
                    dbConn.Dispose();
                    command.Dispose();
                }
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql字符串返回首行首列的值失败!信息:" + exp.Message);
            }
            finally
            {

            }

            try
            {
                return obj.ToString();
            }
            catch
            {
                return "";
            }
        }
        #endregion

        #region 执行sql字符串返回影响的行数
        /// <summary>
        /// 执行sql字符串返回影响的行数(适合于执行Update、Delete和Insert T_SQL语句)
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>影响的行数(失败返回-1)</returns>
        public int ExecuteNonQuery(string sqlText, IDataParameter[] paraList)
        {
            int rowCount = 0;
            try
            {
                using (DBConnection dbConn = new DBConnection())
                {
                    DBCommand command;
                    if (!isProc(sqlText))
                    {
                        command = CreateCommand(sqlText, CommandType.Text, paraList);
                    }
                    else
                    {
                        command = CreateCommand(sqlText, CommandType.StoredProcedure, paraList);
                    }
                    command.Connection = dbConn;

                    dbConn.ConnectionString = strConn;
                    dbConn.Open();

                    rowCount = command.ExecuteNonQuery();

                    dbConn.Close();
                    dbConn.Dispose();
                    command.Dispose();
                }                
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql字符串返回影响的行数失败!信息:" + exp.Message);
            }
            finally
            {
                
            }

            return rowCount;
        }
        #endregion

        #region 执行sql字符串返回数据记录集第一行NameValue集合
        /// <summary>
        /// 执行sql字符串返回数据记录集第一行NameValue集合
        /// </summary>
        /// <param name="sqlText">要执行的T_SQL语句字符串</param>
        /// <param name="paraList">T_SQL语句字符串中所带的参数数组(无参数是paraList为null)</param>
        /// <returns>数据记录集第一行NameValue集合</returns>
        public NameValueCollection ExecuteNameValueCollection(string sqlText, IDataParameter[] paraList)
        {
            DataTable dt = null;
            NameValueCollection nvc = null;
            try
            {
                dt = ExecuteDataTable(sqlText, paraList);

                if (dt.Rows.Count != 0)
                {
                    nvc = new System.Collections.Specialized.NameValueCollection();

                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        nvc[dt.Columns[i].ColumnName] = dt.Rows[0][i].ToString();
                    }
                }

                return nvc;
            }
            catch (Exception exp)
            {
                throw new Exception("执行sql字符串返回影响的行数失败!信息:" + exp.Message);
            }
            finally
            {
                
            }
        }
        #endregion

        #region 事务执行sql命令
        /// <summary>
        /// 事务执行sql命令,只适合执行Update、Delete和Insert T_SQL语句数组
        /// </summary>
        /// <param name="sqlList">T_SQL语句数组</param>
        /// <param name="paraList">T_SQL语句数组对应的参数数组的列表</param>
        /// <returns>是否成功</returns>
        public bool TranExecuteNonQuery(List<string> sqlList, List<IDataParameter[]> paraList)
        {
            if (sqlList.Count == 0)
            {
                return true;
            }
            else
            {
                if (sqlList.Count != paraList.Count)
                {
                    return false;
                }
            }

            IDbTransaction DbTran;
            DBConnection dbConn = new DBConnection();
            dbConn.ConnectionString = strConn;
            try
            {
                dbConn.Open(); 
            }
            catch
            {
                dbConn.Dispose();
                return false;
            }

            DbTran = dbConn.BeginTransaction();

            DBCommand command = CreateCommand();
            command.CommandTimeout = 20;
            command.Transaction = DbTran;
            command.Connection = dbConn;

            try
            {
                for (int i = 0; i < sqlList.Count; i++)
                {
                    command.CommandText = sqlList[i].ToString();
                    if (!isProc(sqlList[i].ToString()))
                    {
                        command.CommandType = CommandType.Text;
                    }
                    else
                    {
                        command.CommandType = CommandType.StoredProcedure;
                    }

                    command.Parameters.Clear();

                    if (paraList[i] != null)
                    {
                        foreach (IDataParameter para in paraList[i])
                        {
                            command.Parameters.Add(para);
                        }
                    }

                    command.ExecuteNonQuery();
                }

                DbTran.Commit();

                DbTran.Dispose();
                dbConn.Close();
                dbConn.Dispose();
                command.Dispose();

                return true;
            }
            catch(Exception ex)
            {
                DbTran.Rollback();

                DbTran.Dispose();
                dbConn.Close();
                dbConn.Dispose();
                command.Dispose();

                return false;
            }
            finally
            {
                
            }
        }
        #endregion

        #region 插入数据
        /// <summary>
        /// 插入数据,返回影响的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="fieldsName">字段名集合</param>
        /// <param name="values">内容集合</param>
        /// <returns>影响记录行数</returns>
        public int DataInsert(string tableName, List<string> fieldsName, List<string> values)
        {
            if (((fieldsName.Count > 0 && values.Count > 0) && fieldsName.Count != values.Count) || tableName.Trim() == "" || values.Count == 0)
            {
                return 0;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append(" INSERT INTO ");
            sql.Append(" " + tableName + " ");

            if (fieldsName.Count > 0)
            {
                sql.Append(" ( ");
                for (int i = 0; i < fieldsName.Count; i++)
                {
                    if (i == fieldsName.Count - 1)
                    {
                        sql.Append(fieldsName[i]);
                    }
                    else
                    {
                        sql.Append(fieldsName[i] + ",");
                    }
                }
                sql.Append(" ) ");
            }

            sql.Append(" VALUES ");

            sql.Append(" ( ");
            for (int i = 0; i < values.Count; i++)
            {
                if (i == values.Count - 1)
                {
                    sql.Append("'" + values[i] + "'");
                }
                else
                {
                    sql.Append("'" + values[i] + "'" + ",");
                }
            }
            sql.Append(" ) ");


            return ExecuteNonQuery(sql.ToString(),null);

        }
        #endregion

        #region 修改数据
        /// <summary>
        /// 修改数据,返回影响的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="fieldsName">字段名集合</param>
        /// <param name="values">内容集合</param>
        /// <param name="conditions">条件集合 不能带有and</param>
        /// <returns>影响记录行数</returns>
        public int DataUpdate(string tableName, List<string> fieldsName, List<string> values, List<string> conditions)
        {
            if (tableName.Trim() == "" || fieldsName.Count == 0 || fieldsName.Count != values.Count)
            {
                return 0;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append(" UPDATE ");
            sql.Append(" " + tableName + " ");

            for (int i = 0; i < fieldsName.Count; i++)
            {
                if (i != fieldsName.Count - 1)
                {
                    sql.Append(" SET " + fieldsName[i] + " = " + "'" + values[i] + "'" + ",");
                }
                else
                {
                    sql.Append(" SET " + fieldsName[i] + " = " + "'" + values[i] + "' ");
                }
            }

            for (int i = 0; i < conditions.Count; i++)
            {
                if (i == 0)
                {
                    sql.Append(" WHERE " + conditions[i]);
                }
                else
                {
                    sql.Append(" AND " + conditions[i]);
                }
            }


            return ExecuteNonQuery(sql.ToString(),null);

        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据,返回影响的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="conditions">条件集合 不能带有and</param>
        /// <returns>影响记录行数</returns>
        public int DataDelete(string tableName, List<string> conditions)
        {
            if (tableName.Trim() == "")
            {
                return 0;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append(" DELETE FROM ");
            sql.Append(" " + tableName + " ");

            for (int i = 0; i < conditions.Count; i++)
            {
                if (i == 0)
                {
                    sql.Append(" WHERE " + conditions[i]);
                }
                else
                {
                    sql.Append(" AND " + conditions[i]);
                }
            }


            return ExecuteNonQuery(sql.ToString(),null);

        }
        #endregion

        #region 获取下一个序号
        /// <summary>
        /// 获取下一个序号(失败返回-1)
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="idName">字段名(必须为number类型)</param>
        /// <returns>下一个序号</returns>
        public int GetNextId(string tableName, string idName)
        {
            int id = 1;
            string sql = "SELECT MAX(" + idName + ") FROM " + tableName;

            try
            {
                if ("" != ExecuteString(sql, null))
                {
                    id = Convert.ToInt32(ExecuteString(sql, null)) + 1;
                }
            }
            catch
            {
                id = -1;
            }

            return id;
        }
        #endregion

        #region 获取下一个序号
        /// <summary>
        /// 获取下一个序号
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="idName">字段名(必须为number类型)</param>
        /// <returns>下一个序号</returns>
        public int GetNextId(string sequenceName)
        {
            int id = 1;
            string sql = "select " + sequenceName + ".Nextval from dual";
            try
            {
                id = Convert.ToInt32(Util.DbFactory.dbOperate.ExecuteString(sql, null));
            }
            catch(Exception ee)
            {
                id = - 1;
            }

            return id;
        }
        #endregion

        #endregion
    }
    #endregion 
}

知识共享许可协议
《C# 数据库操作工厂模式类》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     151.64ms