SqlHelper.cs

//===================================================================
//This file is tony's data access helper for .NET
//For more information please go to http://dotnet.tonygan.com
//===================================================================

using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

namespace Tony.Common
{
 /// <summary>
 /// The SqlHelper class is intended to encapsulate high performance,
 /// scalable best practices for common uses of SqlClient.
 /// </summary>
 public abstract class SqlHelper
 {
  #region private utility methods & constructors

  //Database connection strings
  public static readonly string ConnectionString = ConfigurationSettings.AppSettings["SQLConnectionString"];

  //Since this class provides only static methods, make the default constructor private to prevent
  //instances from being created with "new SqlHelper()".
  private SqlHelper() { }

  #endregion

  # region ExecuteNonQuery

  public int ExecuteNonQuery(string cmdText)
  {
   // Create & open a SqlConnection, and dispose of it after we are done
   using (SqlConnection connection = new SqlConnection(ConnectionString))
   {
    using (SqlCommand command = new SqlCommand(cmdText, connection))
    {
     try
     {
      connection.Open();
      return command.ExecuteNonQuery();
     }
     catch(SqlException ex)
     {
      throw new Exception(ex.Message);
     }
    }
   }
  }

  public static void ExecuteSqlTransaction(ArrayList cmdTextList)
  {
   using (SqlConnection connection = new SqlConnection(ConnectionString))
   {
    connection.Open();
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    SqlTransaction trans = connection.BeginTransaction();
    command.Transaction = trans;
    try
    {
     for (int n = 0; n < cmdTextList.Count; n++)
     {
      string cmdText = cmdTextList[n].ToString();
      if (cmdText.Trim().Length > 1)
      {
       command.CommandText = cmdText;
       command.ExecuteNonQuery();
      }
     }
     trans.Commit();
    }
    catch (SqlException ex)
    {
     trans.Rollback();
     throw new Exception(ex.Message);
    }
   }
  }

  #endregion

  #region ExecuteScalar

  public static object ExecuteScalar(string cmdText)
  {
   using (SqlConnection connection = new SqlConnection(ConnectionString))
   {
    using (SqlCommand command = new SqlCommand(cmdText, connection))
    {
     try
     {
      connection.Open();
      object obj = command.ExecuteScalar();
      if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
      {
       return null;
      }
      else
      {
       return obj;
      }
     }
     catch (SqlException ex)
     {
      connection.Close();
      throw new Exception(ex.Message);
     }
    }
   }
  }

  #endregion

        #region ExecuteReader

  public static SqlDataReader ExecuteReader(string cmdText)
  {
   SqlConnection connection = new SqlConnection(ConnectionString);
   SqlCommand command = new SqlCommand(cmdText, connection);
   try
   {
    connection.Open();
    SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
    return dataReader;
   }
   catch (SqlException ex)
   {
    throw new Exception(ex.Message);
   }
   /*finally //不能在此关闭,否则,返回的对象将无法使用
   {
    command.Dispose();
    connection.Close();
   }*/
  }

  #endregion

        #region ExecuteDataSet

  public static DataSet ExecuteDataSet(string cmdText)
  {
   using (SqlConnection connection = new SqlConnection(ConnectionString))
   {
    DataSet dataSet = new DataSet();
    try
    {
     connection.Open();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdText, connection);
     dataAdapter.Fill(dataSet);
    }
    catch (SqlException ex)
    {
     throw new Exception(ex.Message);
    }
    return dataSet;
   }
  }

    #endregion
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值