Asp.net三层架构之数据访问层(MS SQL 2000版)

  1. DataAccess.cs
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. using System.Text;
  13. /// <summary>
  14. /// DataAccess 的摘要说明
  15. /// </summary>
  16. public class DataAccess
  17. {
  18.     private SqlConnection conn = null;
  19.     private SqlCommand comm = null;
  20.     private SqlDataAdapter da = null;
  21. public DataAccess()
  22. {
  23.         conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  24. }
  25.     // 执行非查询语句,并返回受影响的记录行数
  26.     public int ExecuteCommand(string sql)
  27.     {
  28.         int result = 0;
  29.         comm = new SqlCommand(sql, conn);
  30.         try
  31.         {
  32.             conn.Open();
  33.             result = comm.ExecuteNonQuery();
  34.         }
  35.         catch
  36.         {
  37.             return 0;
  38.         }
  39.         finally
  40.         {
  41.             this.conn.Close();
  42.         }
  43.         return result;
  44.     }
  45.     // 执行非查询语句,并返回受影响的记录行数
  46.     public int ExecuteCommand(string sql, CommandType cmdtype, SqlParameter[] parameters)
  47.     {
  48.         int result = 0;
  49.         comm = new SqlCommand();
  50.         try
  51.         {
  52.             conn.Open();
  53.             comm.Connection = conn;
  54.             comm.CommandText = sql;
  55.             comm.CommandType = cmdtype;
  56.             if (parameters != null && parameters.Length > 0)
  57.             {
  58.                 foreach (SqlParameter param in parameters)
  59.                 {
  60.                     comm.Parameters.Add(param);
  61.                 }
  62.             }
  63.             
  64.             result = comm.ExecuteNonQuery();
  65.         }
  66.         catch
  67.         {
  68.             return 0;
  69.         }
  70.         finally
  71.         {
  72.             this.conn.Close();
  73.         }
  74.         return result;
  75.     }
  76.         
  77.     public int ExecuteCommand(string sql, CommandType cmdtype, SqlParameter[] parameters, ref String[] output)
  78.     {
  79.         int _result = 0;
  80.         
  81.         comm = new SqlCommand();
  82.         try
  83.         {
  84.             conn.Open();
  85.             comm.Connection = conn;
  86.             comm.CommandText = sql;
  87.             comm.CommandType = cmdtype;
  88.             if (parameters != null && parameters.Length > 0)
  89.             {
  90.                 foreach (SqlParameter param in parameters)
  91.                 {
  92.                     comm.Parameters.Add(param);
  93.                 }
  94.             }
  95.             _result = comm.ExecuteNonQuery();
  96.             for (int i = 0; i < output.Length; i++)
  97.             {
  98.                 output[i] = parameters[0].Value.ToString().Trim();
  99.             }
  100.         }
  101.         catch (Exception ex)
  102.         {
  103.             throw new Exception(ex.Message);
  104.         }
  105.         finally
  106.         {
  107.             conn.Close();
  108.         }
  109.         return _result;
  110.     }
  111.     //生成参数
  112.     public SqlParameter CreateParameter(string field, DbType dbtype, string value)
  113.     {
  114.         SqlParameter p = new SqlParameter();
  115.         p.ParameterName = field;
  116.         p.DbType = dbtype;
  117.         p.Value = value;
  118.         return p;
  119.     }
  120.     
  121.     public SqlParameter CreateParameter(string field, DbType dbtype, string value, ParameterDirection direction)
  122.     {
  123.         SqlParameter p = new SqlParameter();
  124.         p.ParameterName = field;
  125.         p.DbType = dbtype;
  126.         p.Value = value;
  127.         p.Direction = direction;
  128.         return p;
  129.     }
  130.     
  131.     // 执行查询,并以DataReader返回结果集
  132.     public SqlDataReader ExecuteReader(string sql)
  133.     {
  134.         SqlDataReader result = null;
  135.         comm = new SqlCommand(sql, conn);
  136.         try
  137.         {
  138.             conn.Open();
  139.             result = comm.ExecuteReader();
  140.         }
  141.         catch
  142.         {
  143.             return null;
  144.         }
  145.                         
  146.         return result;
  147.     }
  148.     // 执行查询,并以DataSet返回结果集
  149.     public DataSet GetDataSet(string sql)
  150.     {
  151.         DataSet ds = new DataSet();
  152.         try
  153.         {
  154.             conn.Open();
  155.             da = new SqlDataAdapter(sql, conn);
  156.             da.Fill(ds);
  157.         }
  158.         catch
  159.         {
  160.             return null;
  161.         }
  162.         finally
  163.         {
  164.             this.conn.Close();
  165.         }
  166.         return ds;
  167.     }
  168.     // 执行查询,并以DataView返回结果集
  169.     public DataView GetDataView(string sql)
  170.     {
  171.         DataView dv = this.GetDataSet(sql).Tables[0].DefaultView;
  172.         return dv;
  173.     }
  174.     // 执行查询,并以DataTable返回结果集
  175.     public DataTable GetDataTable(string sql)
  176.     {
  177.         DataTable dt = GetDataSet(sql).Tables[0];
  178.         return dt;
  179.     }
  180.     //返回首行首列的值
  181.     public object ExecuteScalar(string sql)
  182.     {
  183.         object result = null;
  184.         SqlCommand command = new SqlCommand(sql, conn);
  185.         try
  186.         {
  187.             conn.Open();
  188.             result = command.ExecuteScalar();
  189.         }
  190.         catch
  191.         {
  192.             return null;
  193.         }
  194.         finally
  195.         {
  196.             this.conn.Close();
  197.         }
  198.         return result;
  199.     }
  200. }

常用的工具类

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值