非常好用的sqlhelp(sqlserver、oracle、access)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Configuration;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Diagnostics;
using System.Collections;

namespace Synchronous
{
    public class SqlHelp
    {
        #region 数据库连接字段(web.config来配置)
        public string ConnectStringAcc { get; set; }
        public string ConnectStringSql { get; set; }
        //public string ConnectStringOrl { get; set; }

        public SqlHelp()
        {
//SKY-20191127ETS(我的电脑查看属性)\SQLSERVER2017(win+r  services.msc查看sqlserver括号里的值)
            ConnectStringSql = @"server=SKY-20191127ETS;Integrated Security=True;Persist Security Info=True;Database=F:\库名.MDF;user id=sa;password=123;pooling=false";
            //ConnectStringAcc = ConfigurationManager.ConnectionStrings["acc"].ConnectionString;
            //ConnectStringOrl = ConfigurationManager.AppSettings["ConnectStringOrl"];

        }
        #endregion

        #region  创建Connection
        private IDbConnection GetConnection(string DbType)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return new SqlConnection(ConnectStringSql);
                //case "Oracle":
                //    return new OracleConnection(ConnectStringOrl);
                case "Access":
                    return new OleDbConnection(ConnectStringAcc);
                default:
                    return new SqlConnection(ConnectStringSql);
            }
        }
        #endregion

        #region  创建Command
        private IDbCommand GetCommand(string DbType, string sql, IDbConnection iConn)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return new SqlCommand(sql, (SqlConnection)iConn);
                //case "Oracle":
                //    return new OracleCommand(sql, (OracleConnection)iConn);
                case "Access":
                    return new OleDbCommand(sql, (OleDbConnection)iConn);
                default:
                    return new SqlCommand(sql, (SqlConnection)iConn);
            }
        }
        private IDbCommand GetCommand(string DbType)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return new SqlCommand();
                //case "Oracle":
                //    return new OracleCommand();
                case "Access":
                    return new OleDbCommand();
                default:
                    return new SqlCommand();
            }
        }
        #endregion

        #region 创建Adapter
        private IDataAdapter GetAdapter(string DbType, string sql, IDbConnection iConn)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return new SqlDataAdapter(sql, (SqlConnection)iConn);
                //case "Oracle":
                //    return new OracleDataAdapter(sql, (OracleConnection)iConn);
                case "Access":
                    return new OleDbDataAdapter(sql, (OleDbConnection)iConn);
                default:
                    return new SqlDataAdapter(sql, (SqlConnection)iConn);
            }
        }
        private IDataAdapter GetAdapter(string DbType, IDbCommand iCmd)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return new SqlDataAdapter((SqlCommand)iCmd);
                //case "Oracle":
                //    return new OracleDataAdapter((OracleCommand)iCmd);
                case "Access":
                    return new OleDbDataAdapter((OleDbCommand)iCmd);
                default:
                    return new SqlDataAdapter((SqlCommand)iCmd);
            }
        }
        #endregion

        #region 执行数据库表操作
        public IDataReader ExecuteReader(string DbType, string SqlString)
        {
            IDbConnection iConn = GetConnection(DbType);
            {
                iConn.Open();
                IDbCommand iCmd = GetCommand(DbType);
                {
                    try
                    {
                        PrepareCommand(DbType, out iCmd, iConn, null, SqlString, null);
                        IDataReader iReader = iCmd.ExecuteReader();
                        return iReader;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        iCmd.Dispose();

                    }
                }
            }


        }

        private void PrepareCommand(string DbType, out IDbCommand iCmd, IDbConnection iConn, IDbTransaction iTrans, string cmdText, IDataParameter[] iParms)
        {
            if (iConn.State != ConnectionState.Open)
            {
                iConn.Open();
            }
            iCmd = GetCommand(DbType);
            iCmd.Connection = iConn;
            iCmd.CommandText = cmdText;
            if (iTrans != null)
            {
                iCmd.Transaction = iTrans;
            }
            iCmd.CommandType = CommandType.Text; //cmdType
            if (iParms != null)
            {
                foreach (IDataParameter parm in iParms)
                {
                    iCmd.Parameters.Add(parm);
                }
            }

        }

        //执行查询语句,返回DataSet,重点用于Oracle数据库
        /// 
        /// 执行查询语句,返回DataSet,重点用于Oracle数据库
        /// 
        /// 
        /// 
        /// 
        public DataSet Query(string DbType, string SqlString)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                DataSet Ds = new DataSet();
                iConn.Open();
                try
                {
                    IDataAdapter iAdapter = GetAdapter(DbType, SqlString, iConn);
                    iAdapter.Fill(Ds);
                    return Ds;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (iConn.State != ConnectionState.Closed)
                    {
                        iConn.Close();
                    }
                }
            }
        }

        //执行查询语句,返回DataTable,重点用于Oracle数据库
        /// 
        /// 执行查询语句,返回DataTable,重点用于Oracle数据库
        /// 
        /// 
        /// 
        /// 
        public DataTable ExcuteQuery(string DbType, string SqlString)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                DataSet Ds = new DataSet();
                iConn.Open();
                try
                {
                    IDataAdapter iAdapter = GetAdapter(DbType, SqlString, iConn);
                    iAdapter.Fill(Ds);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (iConn.State != ConnectionState.Closed)
                    {
                        iConn.Close();
                    }
                }
                return Ds.Tables[0];
            }
        }

        //执行数据库表操作,重点用于SqlServer数据库
        /// 
        /// 执行数据库表操作,重点用于SqlServer数据库  Update、Insert和Delete语句
        /// 
        /// 
        /// 
        public int ExcuteSqlScalar(string DbType, string SqlString)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
                {
                    iConn.Open();
                    return iCmd.ExecuteNonQuery();
                }
            }
        }
        /// 
        ///  执行查询,并返回查询所返回的结果集中第一行的第一列
        /// 
        /// 
        /// 
        /// 
        public int ExcuteSqlNum(string DbType, string SqlString)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
                {
                    iConn.Open();
                    int i = (int)iCmd.ExecuteScalar();
                    return i;
                }
            }
        }
        //删除数据
        public void DeleGet(string DbType, string SqlString)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
                {
                    iConn.Open();
                    iCmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region 执行多条SQL语句,实现数据库事物

        /// 
        /// Update、Insert和Delete语句    执行多条语句   事务
        /// 
        /// 
        /// 多条SQL语句
        /// 
        public bool ExcuteSqlTran(string DbType, List SqlStringList)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                iConn.Open();
                using (IDbCommand iCmd = GetCommand(DbType))
                {
                    iCmd.Connection = iConn;
                    using (IDbTransaction iDbTran = iConn.BeginTransaction())
                    {
                        iCmd.Transaction = iDbTran;
                        try
                        {
                            for (int n = 0; n < SqlStringList.Count; n++)
                            {
                                string StrSql = SqlStringList[n].ToString();
                                if (StrSql.Trim().Length > 1)
                                {
                                    iCmd.CommandText = StrSql;
                                    iCmd.ExecuteNonQuery();
                                }
                            }
                            iDbTran.Commit();
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("ExcuteSqlTran" + ex.Message);
                            iDbTran.Rollback();
                            return false;
                        }
                        finally
                        {
                            if (iConn.State != ConnectionState.Closed)
                            {
                                iConn.Close();
                            }
                        }
                    }
                }
                return true;
            }
        }


        #endregion

        #region SQL数据库解析格式
        private IDbDataParameter DbPara(string DbType, string ParaName, string DataType)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return GetSqlPara(ParaName, DataType);
                //case "Oracle":
                //    return GetOraclePara(ParaName, DbType);
                case "Access":
                    return GetOledbPara(ParaName, DbType);
                default:
                    return GetSqlPara(ParaName, DbType);
            }
        }

        private IDbDataParameter GetSqlPara(string ParaName, string DataType)
        {
            switch (DataType)
            {
                case "Decimal":
                    return new SqlParameter(ParaName, SqlDbType.Decimal);
                case "VarChar":
                    return new SqlParameter(ParaName, SqlDbType.VarChar);
                case "DateTime":
                    return new SqlParameter(ParaName, SqlDbType.DateTime);
                case "Image":
                    return new SqlParameter(ParaName, SqlDbType.Image);
                case "Int":
                    return new SqlParameter(ParaName, SqlDbType.Int);
                case "Text":
                    return new SqlParameter(ParaName, SqlDbType.Text);
                default:
                    return new SqlParameter(ParaName, SqlDbType.VarChar);
            }
        }

        Oracle数据库解析数据格式
        //private IDbDataParameter GetOraclePara(string ParaName, string DbType)
        //{
        //    switch (DbType)
        //    {
        //        case "Decimal":
        //            return new OracleParameter(ParaName, SqlDbType.Decimal);
        //        case "VarChar":
        //            return new OracleParameter(ParaName, SqlDbType.VarChar);
        //        case "DateTime":
        //            return new OracleParameter(ParaName, SqlDbType.DateTime);
        //        case "Image":
        //            return new OracleParameter(ParaName, SqlDbType.Image);
        //        case "Int":
        //            return new OracleParameter(ParaName, SqlDbType.Int);
        //        case "Text":
        //            return new OracleParameter(ParaName, SqlDbType.Text);
        //        default:
        //            return new OracleParameter(ParaName, SqlDbType.VarChar);
        //    }
        //}

        //Access(OLEDB)数据库数据解析格式
        private IDbDataParameter GetOledbPara(string ParaName, string DbType)
        {
            switch (DbType)
            {
                case "Decimal":
                    return new OleDbParameter(ParaName, SqlDbType.Decimal);
                case "VarChar":
                    return new OleDbParameter(ParaName, SqlDbType.VarChar);
                case "DateTime":
                    return new OleDbParameter(ParaName, SqlDbType.DateTime);
                case "Image":
                    return new OleDbParameter(ParaName, SqlDbType.Image);
                case "Int":
                    return new OleDbParameter(ParaName, SqlDbType.Int);
                case "Text":
                    return new OleDbParameter(ParaName, SqlDbType.Text);
                default:
                    return new OleDbParameter(ParaName, SqlDbType.VarChar);
            }
        }

        #endregion

        #region  执行存储过程
        public DataSet RunProcedure(string DbType, string StoreProcName, IDataParameter[] Parameters, DataSet dataSet, string TableName)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                iConn.Open();
                IDataAdapter iDa = GetAdapter1(DbType, BuildQueryCommand(DbType, iConn, StoreProcName, Parameters));
                ((SqlDataAdapter)iDa).Fill(dataSet, TableName);
                if (iConn.State != ConnectionState.Closed)
                {
                    iConn.Close();
                }
                return dataSet;
            }
        }


        private IDbCommand BuildQueryCommand(string dbType, IDbConnection iConn, string storeProcName, IDataParameter[] parameters)
        {
            IDbCommand iCmd = GetCommand(dbType, storeProcName, iConn);
            iCmd.CommandType = CommandType.StoredProcedure;
            if (parameters == null)
            {
                return iCmd;
            }
            foreach (IDataParameter parameter in parameters)
            {
                iCmd.Parameters.Add(parameter);
            }
            return iCmd;
        }

        private IDataAdapter GetAdapter1(string DbType, IDbCommand iCmd)
        {
            switch (DbType)
            {
                case "SqlServer":
                    return new SqlDataAdapter((SqlCommand)iCmd);
                //case "Oracle":
                //    return new OracleDataAdapter((OracleCommand)iCmd);
                case "Oledb":
                    return new OleDbDataAdapter((OleDbCommand)iCmd);
                default:
                    return new SqlDataAdapter((SqlCommand)iCmd);
            }
        }

        /// 
        /// 执行存储过程
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public DataSet RunProcedure(string DbType, string storeProcName, IDbDataParameter[] parameters, string TableName)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                DataSet Ds = new DataSet();
                iConn.Open();
                IDataAdapter iDa = GetAdapter(DbType, BuildQueryCommand(DbType, iConn, storeProcName, parameters));
                ((SqlDataAdapter)iDa).Fill(Ds, TableName);
                if (iConn.State != ConnectionState.Closed)
                {
                    iConn.Close();
                }
                return Ds;
            }
        }

        public DataSet RunProcedure(string DbType, string storeProcName, IDbDataParameter[] parameters)
        {
            using (IDbConnection iConn = GetConnection(DbType))
            {
                DataSet Ds = new DataSet();
                IDataAdapter iDa = GetAdapter(DbType, BuildQueryCommand(DbType, iConn, storeProcName, parameters));
                ((SqlDataAdapter)iDa).Fill(Ds);
                if (iConn.State != ConnectionState.Closed)
                {
                    iConn.Close();
                }
                return Ds;
            }
        }
        #endregion

    }
}

配置文件



     
        
    
  

    

    
    
  

 

 

你可能感兴趣的:(C#,SQL,oracle)