微软DbHelper

using System;

using System.Configuration;

using System.Data;

using System.Data.Common;

using System.Data.OleDb;

using System.Data.SqlClient;





namespace JADAY.Frame.DbHelper

{

    /// <summary>

    /// 数据库助手类

    /// </summary>  

    public class DbUtility

    {

        private static string dbProviderName = ConfigurationSettings.AppSettings["DbProviderName"];

        private static string dbConnectionString = ConfigurationManager.AppSettings["TaxInvoiceConn"];



        private DbConnection connection;



        public DbUtility()

        {

            this.connection = CreateConnection(DbUtility.dbConnectionString);

        }



        public DbUtility(string connectionString)

        {

            this.connection = CreateConnection(connectionString);

        }



        public static DbConnection CreateConnection()

        {

            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbUtility.dbProviderName);

            DbConnection dbconn = dbfactory.CreateConnection();

            dbconn.ConnectionString = DbUtility.dbConnectionString;

            return dbconn;

        }



        public static DbConnection CreateConnection(string connectionString)

        {

            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbUtility.dbProviderName);

            DbConnection dbconn = dbfactory.CreateConnection();

            dbconn.ConnectionString = connectionString;

            return dbconn;

        }



        public DbCommand GetStoredProcCommond(string storedProcedure)

        {

            DbCommand dbCommand = connection.CreateCommand();

            dbCommand.CommandText = storedProcedure;

            dbCommand.CommandType = CommandType.StoredProcedure;

            return dbCommand;

        }



        public DbCommand GetSqlStringCommond(string sqlQuery)

        {

            DbCommand dbCommand = connection.CreateCommand();

            dbCommand.CommandText = sqlQuery;

            dbCommand.CommandType = CommandType.Text;

            return dbCommand;

        }



        #region 增加参数



        public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)

        {

            foreach (DbParameter dbParameter in dbParameterCollection)

            {

                cmd.Parameters.Add(dbParameter);

            }

        }



        public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)

        {

            DbParameter dbParameter = cmd.CreateParameter();

            dbParameter.DbType = dbType;

            dbParameter.ParameterName = parameterName;

            dbParameter.Size = size;

            dbParameter.Direction = ParameterDirection.Output;

            cmd.Parameters.Add(dbParameter);

        }



        public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)

        {

            DbParameter dbParameter = cmd.CreateParameter();

            dbParameter.DbType = dbType;

            dbParameter.ParameterName = parameterName;

            dbParameter.Value = value;

            dbParameter.Direction = ParameterDirection.Input;

            cmd.Parameters.Add(dbParameter);

        }



        public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)

        {

            DbParameter dbParameter = cmd.CreateParameter();

            dbParameter.DbType = dbType;

            dbParameter.ParameterName = parameterName;

            dbParameter.Direction = ParameterDirection.ReturnValue;

            cmd.Parameters.Add(dbParameter);

        }



        public DbParameter GetParameter(DbCommand cmd, string parameterName)

        {

            return cmd.Parameters[parameterName];

        }



        #endregion



        #region 执行



        public DataSet ExecuteDataSet(DbCommand cmd)

        {

            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbUtility.dbProviderName);

            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();

            dbDataAdapter.SelectCommand = cmd;

            DataSet ds = new DataSet();

            dbDataAdapter.Fill(ds);

            return ds;

        }



        public DataTable ExecuteDataTable(DbCommand cmd)

        {

            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbUtility.dbProviderName);

            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();

            dbDataAdapter.SelectCommand = cmd;

            DataTable dataTable = new DataTable();

            dbDataAdapter.Fill(dataTable);

            return dataTable;

        }





        public DbDataReader ExecuteReader(DbCommand cmd)

        {

            cmd.Connection.Open();

            DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            return reader;

        }



        public int ExecuteNonQuery(DbCommand cmd)

        {

            cmd.Connection.Open();

            int ret = cmd.ExecuteNonQuery();

            cmd.Connection.Close();

            return ret;

        }



        public object ExecuteScalar(DbCommand cmd)

        {

            cmd.Connection.Open();

            object ret = cmd.ExecuteScalar();

            cmd.Connection.Close();

            return ret;

        }



        #endregion



        #region 执行事务



        public DataSet ExecuteDataSet(DbCommand cmd, Trans t)

        {

            cmd.Connection = t.DbConnection;

            cmd.Transaction = t.DbTrans;

            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbUtility.dbProviderName);

            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();

            dbDataAdapter.SelectCommand = cmd;

            DataSet ds = new DataSet();

            dbDataAdapter.Fill(ds);

            return ds;

        }



        public DataTable ExecuteDataTable(DbCommand cmd, Trans t)

        {

            cmd.Connection = t.DbConnection;

            cmd.Transaction = t.DbTrans;

            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbUtility.dbProviderName);

            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();

            dbDataAdapter.SelectCommand = cmd;

            DataTable dataTable = new DataTable();

            dbDataAdapter.Fill(dataTable);

            return dataTable;

        }



        public DbDataReader ExecuteReader(DbCommand cmd, Trans t)

        {

            cmd.Connection.Close();

            cmd.Connection = t.DbConnection;

            cmd.Transaction = t.DbTrans;

            DbDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();

            return reader;

        }



        public int ExecuteNonQuery(DbCommand cmd, Trans t)

        {

            cmd.Connection.Close();

            cmd.Connection = t.DbConnection;

            cmd.Transaction = t.DbTrans;

            int ret = cmd.ExecuteNonQuery();

            return ret;

        }



        public object ExecuteScalar(DbCommand cmd, Trans t)

        {

            cmd.Connection.Close();

            cmd.Connection = t.DbConnection;

            cmd.Transaction = t.DbTrans;

            object ret = cmd.ExecuteScalar();

            return ret;

        }



        #endregion

    }



    public class Trans : IDisposable

    {

        private DbConnection conn;

        private DbTransaction dbTrans;

        public DbConnection DbConnection

        {

            get { return this.conn; }

        }



        public DbTransaction DbTrans

        {

            get { return this.dbTrans; }

        }





        public Trans()

        {

            conn = DbUtility.CreateConnection();

            conn.Open();

            dbTrans = conn.BeginTransaction();

        }



        public Trans(string connectionString)

        {

            conn = DbUtility.CreateConnection(connectionString);

            conn.Open();

            dbTrans = conn.BeginTransaction();

        }



        public void Commit()

        {

            dbTrans.Commit();

            this.Colse();

        }



        public void RollBack()

        {

            dbTrans.Rollback();

            this.Colse();

        }



        public void Dispose()

        {

            this.Colse();

        }



        public void Colse()

        {

            if (conn.State == System.Data.ConnectionState.Open)

            {

                conn.Close();

            }

        }

    }

}

2、直接执行sql语句

 
   
  1. DbHelper db = new DbHelper();  
  2.   
  3. DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values(‘?haha‘?)");  
  4.   
  5. db.ExecuteNonQuery(cmd);  
 
   

3、执行存储过程

 
   
  1. DbHelper db = new DbHelper();  
  2.   
  3. DbCommand cmd = db.GetStoredProcCommond("t1_insert");  
  4.   
  5. db.AddInParameter(cmd, "@id", DbType.String, "heihei");  
  6.   
  7. db.ExecuteNonQuery(cmd);  
 
   

4、返回DataSet

 
   
  1. DbHelper db = new DbHelper();  
  2.   
  3. DbCommand cmd = db.GetSqlStringCommond("select * from t1");  
  4.   
  5. DataSet ds = db.ExecuteDataSet(cmd);  
 
   

5、返回DataTable

 
   
  1. DbHelper db = new DbHelper();  
  2.   
  3. DbCommand cmd = db.GetSqlStringCommond("t1_findall");  
  4.   
  5. DataTable dt = db.ExecuteDataTable(cmd);  
 
   

 

 
   

6、输入参数/输出参数/返回值的使用(比较重要哦)

 
   
  1. DbHelper db = new DbHelper();  
  2.   
  3. DbCommand cmd = db.GetStoredProcCommond("t2_insert");  
  4.   
  5. db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);  
  6.   
  7. db.AddOutParameter(cmd, "@outString", DbType.String, 20);  
  8.   
  9. db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);  
  10.   
  11.   
  12. db.ExecuteNonQuery(cmd);  
  13.   
  14.   
  15. string s = db.GetParameter(cmd, "@outString").Value as string;   
  16.   
  17. int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);   
 
   

 

 
   

7、DataReader使用

 
   

 

 
   
  1. DbHelper db = new DbHelper();  
  2.   
  3. DbCommand cmd = db.GetStoredProcCommond("t2_insert");  
  4.   
  5. db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);  
  6.   
  7. db.AddOutParameter(cmd, "@outString", DbType.String, 20);  
  8.   
  9. db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);  
  10.   
  11.   
  12. using (DbDataReader reader = db.ExecuteReader(cmd))  
  13.   
  14. {  
  15.   
  16. dt.Load(reader);  
  17.   
  18. }   
  19.   
  20. string s = db.GetParameter(cmd, "@outString").Value as string;   
  21.   
  22. int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);   
 
   

 

 
   

8、事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)

 
   

以上我们好像没有指定数据库连接字符串,大家如果看下DbHelper的代码,就知道要使用它必须在config中配置两个参数,如下:

 
   
  1. pubic void DoBusiness()  
  2.   
  3. {  
  4.   
  5. using (Trans t = new Trans())  
  6.   
  7. {  
  8.   
  9. try  
  10.   
  11. {  
  12.   
  13. D1(t);  
  14.   
  15. throw new Exception();  
  16.   
  17. D2(t);  
  18.   
  19. t.Commit();  
  20.   
  21. }  
  22.   
  23. catch  
  24.   
  25. {  
  26.   
  27. t.RollBack();  
  28.   
  29. }  
  30.   
  31. }  
  32.   
  33. }  
  34.   
  35. public void D1(Trans t)  
  36.   
  37. {  
  38.   
  39. DbHelper db = new DbHelper();  
  40.   
  41. DbCommand cmd = db.GetStoredProcCommond("table_insert");  
  42.   
  43. db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);  
  44.   
  45. db.AddOutParameter(cmd, "@outString", DbType.String, 20);  
  46.   
  47. db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);  
  48.   
  49.   
  50. if (t == null) db.ExecuteNonQuery(cmd);  
  51.   
  52. else db.ExecuteNonQuery(cmd,t);  
  53.   
  54.   
  55. string s = db.GetParameter(cmd, "@outString").Value as string;  
  56.   
  57. int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);  
  58.   
  59. }  
  60.   
  61. public void D2(Trans t)  
  62.   
  63. {  
  64.   
  65. DbHelper db = new DbHelper();  
  66.   
  67. DbCommand cmd = db.GetSqlStringCommond("insert table (id)values(@id)");   
  68.   
  69. if (t == null) db.ExecuteNonQuery(cmd);  
  70.   
  71. else db.ExecuteNonQuery(cmd, t);  
  72.   
  73. }  
 

 

你可能感兴趣的:(help)