SqlDataConnect.cs
上传用户:tiancihang
上传日期:2014-03-12
资源大小:21387k
文件大小:10k
源码类别:

.net编程

开发平台:

C#

  1. using System;
  2. using System.Xml;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Configuration;
  6. using System.Collections;
  7. /// <summary>
  8. /// SqlDataConnect 的摘要说明
  9. /// SqlParameterCollection
  10. /// </summary>
  11. namespace com.etong.SqlDataConnect
  12. {
  13.     /// <summary>
  14.     /// MSSqlDataAccess 的摘要说明。
  15.     /// </summary>
  16.     public class MSSqlDataAccess
  17.     {
  18.         private SqlConnection _dbconn;     ///定义连接;
  19.         private SqlTransaction trans = null; ///定义事务; 
  20.         /// <summary>
  21.         /// 构造函数
  22.         /// </summary>
  23.         public MSSqlDataAccess(SqlConnection conn)
  24.         {
  25.             this._dbconn = conn;
  26.         }
  27.         public MSSqlDataAccess(string connstr)
  28.         {
  29.             this._dbconn = new SqlConnection(connstr);
  30.         }
  31.         public MSSqlDataAccess(int i)
  32.         {
  33.             string sqlconnstr;
  34.             if (i == 2)
  35.             {
  36.                 sqlconnstr = System.Configuration.ConfigurationManager.AppSettings["SqlConnection2"];
  37.                 //sqlconnstr = System.Configuration.ConfigurationManager.ConnectionStrings["connectionstring2"].ConnectionString;
  38.             }
  39.             else
  40.             {
  41.                 sqlconnstr = System.Configuration.ConfigurationManager.AppSettings["SqlConnection"];
  42.                 //sqlconnstr = System.Configuration.ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
  43.             }
  44.             this._dbconn = new SqlConnection(sqlconnstr);
  45.         }
  46.         /// <summary>
  47.         /// 取得连接
  48.         /// </summary>
  49.         public SqlConnection DbConnection
  50.         {
  51.             get
  52.             {
  53.                 return _dbconn;
  54.             }
  55.         }
  56.         /// <summary>
  57.         /// 打开连接
  58.         /// </summary>
  59.         public void Open()
  60.         {
  61.             DbConnection.Open();
  62.         }
  63.         /// <summary>
  64.         /// 关闭连接
  65.         /// </summary>
  66.         public void Close()
  67.         {
  68.             DbConnection.Close();
  69.         }
  70.         /// <summary>
  71.         /// 判断连接是否打开
  72.         /// </summary>
  73.         public bool IsClosed()
  74.         {
  75.             return DbConnection.State.Equals(ConnectionState.Closed);
  76.         }
  77.         /// <summary>
  78.         /// 开始事务,并返回事务对象
  79.         /// </summary>
  80.         public SqlTransaction BeginTransaction()
  81.         {
  82.             trans = this._dbconn.BeginTransaction();
  83.             return trans;
  84.         }
  85.         public int ExecuteNonQuery(CommandType commandType, string commandText, QueryParameterCollection commandParameters)
  86.         {
  87.             SqlCommand cmd = new SqlCommand();
  88.             PrepareCommand(cmd, commandType, commandText, commandParameters);
  89.             int tmpValue = cmd.ExecuteNonQuery();
  90.             cmd.Parameters.Clear();
  91.             return tmpValue;
  92.         }
  93.         public DataSet ExecuteDataset(CommandType commandType, string commandText, QueryParameterCollection commandParameters, DataSet ds, string tableName)
  94.         {
  95.             SqlCommand cmd = new SqlCommand();
  96.             PrepareCommand(cmd, commandType, commandText, commandParameters);
  97.             SqlDataAdapter da = new SqlDataAdapter(cmd);
  98.             da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  99.             if (Object.Equals(tableName, null) || (tableName.Length < 1))
  100.                 da.Fill(ds);
  101.             else
  102.                 da.Fill(ds, tableName);
  103.             cmd.Parameters.Clear();
  104.             return ds;
  105.         }
  106.         public SqlDataReader ExecuteReader(CommandType commandType, string commandText, QueryParameterCollection commandParameters)
  107.         {
  108.             SqlCommand cmd = new SqlCommand();
  109.             PrepareCommand(cmd, commandType, commandText, commandParameters);
  110.             SqlDataReader dr = cmd.ExecuteReader();
  111.             cmd.Parameters.Clear();
  112.             return dr;
  113.         }
  114.         public object ExecuteScalar(CommandType commandType, string commandText, QueryParameterCollection commandParameters)
  115.         {
  116.             SqlCommand cmd = new SqlCommand();
  117.             PrepareCommand(cmd, commandType, commandText, commandParameters);
  118.             object tmpValue = cmd.ExecuteScalar();
  119.             cmd.Parameters.Clear();
  120.             return tmpValue;
  121.         }
  122.         public XmlReader ExecuteXmlReader(CommandType commandType, string commandText, QueryParameterCollection commandParameters)
  123.         {
  124.             SqlCommand cmd = new SqlCommand();
  125.             PrepareCommand(cmd, commandType, commandText, commandParameters);
  126.             XmlReader reader = cmd.ExecuteXmlReader();
  127.             cmd.Parameters.Clear();
  128.             return reader;
  129.         }
  130.         public DataView ExecuteDataView(CommandType commandType, string commandText, QueryParameterCollection commandParameters)
  131.         {
  132.             DataSet ds = new DataSet();
  133.             SqlCommand cmd = new SqlCommand();
  134.             PrepareCommand(cmd, commandType, commandText, commandParameters);
  135.             SqlDataAdapter da = new SqlDataAdapter(cmd);
  136.             da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  137.             da.Fill(ds, "MyTableName");
  138.             cmd.Parameters.Clear();
  139.             return ds.Tables["MyTableName"].DefaultView;
  140.             //ds = this.ExecuteDataset(commandType,commandText,commandParameters,ds,"MyTableName");
  141.             //return ds.Tables["MyTableName"].DefaultView;
  142.         }
  143.         private void PrepareCommand(SqlCommand cmd, CommandType commandType, string commandText, QueryParameterCollection commandParameters)
  144.         {
  145.             cmd.CommandType = commandType;
  146.             cmd.CommandText = commandText;
  147.             cmd.Connection = this._dbconn;
  148.             if (trans != null) cmd.Transaction = trans;
  149.             if ((commandParameters != null) && (commandParameters.Count > 0))
  150.             {
  151.                 for (int i = 0; i < commandParameters.Count; i++)
  152.                 {
  153.                     cmd.Parameters.AddWithValue(commandParameters[i].ParameterName, commandParameters[i].Value);
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     /// <summary>
  159.     /// QueryParameter 的摘要说明。
  160.     /// </summary>
  161.     public sealed class QueryParameter : MarshalByRefObject
  162.     {
  163.         public QueryParameter(string ParameterName, object Value)
  164.         {
  165.             this.m_ParameterName = ParameterName;
  166.             this.m_Value = Value;
  167.         }
  168.         private string m_ParameterName;
  169.         public string ParameterName
  170.         {
  171.             get { return this.m_ParameterName; }
  172.             set { this.m_ParameterName = value; }
  173.         }
  174.         private object m_Value;
  175.         public object Value
  176.         {
  177.             get { return this.m_Value; }
  178.             set { this.m_Value = value; }
  179.         }
  180.     }
  181.     /// <summary>
  182. /// QueryParameterCollection 的摘要说明。
  183. /// </summary>
  184.     public sealed class QueryParameterCollection : MarshalByRefObject
  185.     {
  186.         int intitialCapacity = 10;
  187.         public QueryParameterCollection()
  188.         {
  189.         }
  190.         public QueryParameterCollection(int initCapacity)
  191.         {
  192.             intitialCapacity = initCapacity;
  193.         }
  194.         private ArrayList items;
  195.         private ArrayList ArrayList()
  196.         {
  197.             if (this.items == null)
  198.             {
  199.                 this.items = new ArrayList(intitialCapacity);
  200.             }
  201.             return this.items;
  202.         }
  203.         public int Count
  204.         {
  205.             get
  206.             {
  207.                 if (this.items == null)
  208.                 {
  209.                     return 0;
  210.                 }
  211.                 return this.items.Count;
  212.             }
  213.         }
  214.         public QueryParameter Add(QueryParameter param)
  215.         {
  216.             this.ArrayList().Add(param);
  217.             return param;
  218.         }
  219.         public QueryParameter Add(string ParameterName, object Value)
  220.         {
  221.             return this.Add(new QueryParameter(ParameterName, Value));
  222.         }
  223.         private void Replace(int index, QueryParameter newValue)
  224.         {
  225.             this.Validate(index, newValue);
  226.             this.items[index] = newValue;
  227.         }
  228.         public QueryParameter this[int index]
  229.         {
  230.             get
  231.             {
  232.                 this.RangeCheck(index);
  233.                 return ((QueryParameter)this.items[index]);
  234.             }
  235.             set
  236.             {
  237.                 this.RangeCheck(index);
  238.                 this.Replace(index, value);
  239.             }
  240.         }
  241.         public QueryParameter this[string ParameterName]
  242.         {
  243.             get
  244.             {
  245.                 int num1 = this.RangeCheck(ParameterName);
  246.                 return ((QueryParameter)this.items[num1]);
  247.             }
  248.             set
  249.             {
  250.                 int num1 = this.RangeCheck(ParameterName);
  251.                 this.Replace(num1, value);
  252.             }
  253.         }
  254.         private void ValidateType(object Value)
  255.         {
  256.         }
  257.         private void Validate(int index, QueryParameter Value)
  258.         { }
  259.         private void RangeCheck(int index)
  260.         {
  261.             if ((index < 0) || (this.Count <= index))
  262.             {
  263.                 throw new IndexOutOfRangeException("Number " + index.ToString() + " is out of Range");
  264.             }
  265.         }
  266.         private int RangeCheck(string ParameterName)
  267.         {
  268.             int num1;
  269.             num1 = this.IndexOf(ParameterName);
  270.             if (num1 < 0)
  271.             {
  272.                 throw new IndexOutOfRangeException("ParameterName " + ParameterName + " dose not exist");
  273.             }
  274.             return num1;
  275.         }
  276.         public int IndexOf(string ParameterName)
  277.         {
  278.             int index = -1;
  279.             if (this.items != null)
  280.             {
  281.                 for (int i = 0; i < this.items.Count; i++)
  282.                 {
  283.                     if (((QueryParameter)items[i]).ParameterName.Equals(ParameterName))
  284.                     {
  285.                         index = i;
  286.                         break;
  287.                     }
  288.                 }
  289.             }
  290.             return index;
  291.         }
  292.         public void Clear()
  293.         {
  294.             this.ArrayList().Clear();
  295.         }
  296.     }
  297. }