Combining SqlConnection and SqlCommand in one Class in C#
来源:互联网 发布:数控机床如何编程 编辑:程序博客网 时间:2024/06/06 01:23
原文:http://www.codeproject.com/Tips/826963/Combining-SqlConnection-and-SqlCommand-in-one-Clas
#define ActivateTransactionusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Reflection;/// <summary>/// Summary description for Database/// </summary>/// namespace CustomDatabase{ #region All Enum public enum CommandTypeEnum { /// <summary> /// /// </summary> Text, /// <summary> /// /// </summary> StoredProcedure } public enum CommandMode { NonQuery, Scalar, Reader } #endregion public class Database : IDisposable { private string m_sConnectionString; private string strSqlCommandText; private SqlConnection Conn = null; private SqlTransaction oTrans = null; private object oScalar; List<Parameters> ParameterList = new List<Parameters>(); public Database() { } protected internal Database(string sConnectionString) { m_sConnectionString = sConnectionString; } protected internal string CommandText { set { strSqlCommandText = value; } } private string ConnectionString { get { return m_sConnectionString; } } /// <summary> /// Call or Execute all Reader /// </summary> protected internal int ExecuteCommandReader(CommandTypeEnum sqlType, ref DataTable oTable) { int rowAffected; return ExecuteCommand(CommandMode.Reader, sqlType, ref oScalar, out rowAffected, ref oTable); } protected internal int ExecuteCommandReader(ref DataTable oTable) { int rowAffected; object oScalar = null; return ExecuteCommand(CommandMode.Reader, CommandTypeEnum.StoredProcedure, ref oScalar, out rowAffected, ref oTable); } /// <summary> /// Call or Execute all Scalar /// </summary> protected internal int ExecuteCommandScalar() { int rowAffected; DataTable oTable = new DataTable(); return ExecuteCommand(CommandMode.Scalar, CommandTypeEnum.StoredProcedure, ref oScalar, out rowAffected, ref oTable); } protected internal object ExecuteCommandScalar(ref object eScalar) { int rowAffected; DataTable oTable = new DataTable(); ExecuteCommand(CommandMode.Scalar, CommandTypeEnum.StoredProcedure, ref eScalar, out rowAffected, ref oTable); return oScalar; } /// <summary> /// Call or Execute all Non Query /// </summary> /// protected internal int ExecuteCommandNonQuery(CommandTypeEnum sqlType, out int nRowsAffected) { DataTable oDataTable = new DataTable(); return ExecuteCommand(CommandMode.NonQuery, sqlType, ref oScalar, out nRowsAffected, ref oDataTable); } protected internal int ExecuteCommandNonQuery(out int nRowsAffected) { DataTable oDataTable = new DataTable(); return ExecuteCommand(CommandMode.NonQuery, CommandTypeEnum.StoredProcedure, ref oScalar, out nRowsAffected, ref oDataTable); } #region ExecuteCommands private int ExecuteCommand(CommandMode eMode, CommandTypeEnum sqlType, ref object ScalarOutput, out int nRowsAffected, ref DataTable oTable) { if (Conn != null) { if (Conn.State == ConnectionState.Open) { using (SqlCommand oCmd = new SqlCommand()) { oCmd.CommandText = strSqlCommandText; switch (sqlType) { case CommandTypeEnum.StoredProcedure: oCmd.CommandType = CommandType.StoredProcedure; break; case CommandTypeEnum.Text: oCmd.CommandType = CommandType.Text; break; } foreach (Parameters List in ParameterList) { SqlParameter oParam = new SqlParameter(List.ParameterName, List.ParameterValues); oParam.DbType = List.ParameterType; oCmd.Parameters.Add(oParam); } SqlParameter oRetParam = new SqlParameter("RETURN_VALUE", DBNull.Value); oRetParam.Direction = ParameterDirection.ReturnValue; oCmd.Parameters.Add(oRetParam); oCmd.Connection = Conn; nRowsAffected = 0; switch (eMode) { case CommandMode.NonQuery: nRowsAffected = oCmd.ExecuteNonQuery(); break; case CommandMode.Scalar: ScalarOutput = oCmd.ExecuteScalar(); break; case CommandMode.Reader: SqlDataAdapter oAdapter = new SqlDataAdapter(oCmd); if (oTable != null) { oAdapter.Fill(oTable); } break; }#if ActivateTransaction if (oTrans != null) { oCmd.Transaction = oTrans; }#endif return Convert.ToInt32(oCmd.Parameters["RETURN_VALUE"].Value); } } else throw new DataException("Connection not open"); } else throw new DataException("Connection not open"); } #endregion protected internal void Open() { try { Conn = new SqlConnection(); Conn.ConnectionString = ConnectionString; Conn.Open(); } catch (Exception ex) { throw ex; } } /// <summary> /// Close the connection to the Sql db /// </summary> protected internal void Close() { if (Conn != null) { Conn.Close(); Conn = null; } } protected internal void AddParameter(string Name, object value, DbType Type) { ParameterList.Add(new Parameters() { ParameterName = Name, ParameterType = Type, ParameterValues = value }); }#if ActivateTransaction /// <summary> /// Begins a Transaction /// </summary> protected internal void BeginTransaction() { if (Conn != null) { oTrans = Conn.BeginTransaction(); } else throw new DataException("Connection not open"); } /// <summary> /// Commits an active transaction /// </summary> protected internal void CommitTransaction() { if (oTrans != null) { oTrans.Commit(); oTrans = null; } else throw new DataException("Not in transcation"); } /// <summary> /// Rollbacks an active transaction /// </summary> protected internal void RollbackTransaction() { if (oTrans != null) { oTrans.Rollback(); oTrans = null; } else throw new DataException("Not in transcation"); }#endif #region Connection Dispose public void Dispose() { if (Conn != null) { Conn.Dispose(); } } #endregion } public class Parameters { public string ParameterName { get; set; } public DbType ParameterType { get; set; } public object ParameterValues { get; set; } }}
0 0
- Combining SqlConnection and SqlCommand in one Class in C#
- Combining Navigation Drawer with ViewPager and TabLayout in one screen
- in-mapper combining
- how to blend VB and C# in one project
- C# II: Class ViewModelBase and RelayCommand in MVVM
- 校园英语单词 one in the class
- objc[2213]: Class _NSZombie_BSXPCMessage is implemented in both ?? and ??. One of the two will be us
- objc[2213]: Class _NSZombie_BSXPCMessage is implemented in both ?? and ??. One of the two will be us
- In .net Class and Struct
- 理解SqlConnection,SqlCommand,SqldataReader
- SqlConnection,SqlCommand,SqldataReader
- SqlConnection,SqlDataAdapter,SqlCommand,SqlParameter
- Essential English Grammar And Idioms In One
- C# Call another constructor in one constructor
- The Idea of Combining Random Matrix and Graphical Models in Machine Learning
- Intro:Combining conditional statement in php
- Exceptions in Java and C#
- Properties and Indexers in C#
- 整理看过的书中,提到的别的书。
- 回溯法解决八皇后问题
- 创新驱动磷肥行业转型
- android中的智能指针
- oracle 存储过程的输出
- Combining SqlConnection and SqlCommand in one Class in C#
- IntelliJ IDEA 快捷键和设置
- WebBrowser用法总结
- click 与 jsClick
- UIResponder详解
- android登录Web以及登录保持、cookie管理相关
- 字符串的排列(去除重复项)
- GCD介绍
- Unicode的C/C++编程