ADO.NET数据库操作类

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DBHelper.utility
{
 public class SQLHelper
 {
   
  public SQLHelper()
  {
   
  }

  public static SqlConnection GetConn()
  {
   SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["constr"]);
   conn.Open();
   return conn;
  }

  public static void CloseConn(SqlConnection conn)
  {
   if(conn != null)
   {
    conn.Close();
    conn.Dispose();
   }

  }
  
  public static int ExecuteNonQuery(CommandType cmdType,string cmdText,params SqlParameter[] cmdParams)
  {
   SqlConnection conn = SQLHelper.GetConn();
   SqlCommand cmd = new SqlCommand();
   PrepareCommand(conn,null,cmd,cmdType,cmdText,cmdParams);
   int val;
   try
   {
    val=cmd.ExecuteNonQuery();
   }
   catch
   {
    throw;
   }
   finally
   {
    cmd.Parameters.Clear();
    CloseConn(conn);
   }
   return val;
  }

  
  public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
  {
   SqlCommand cmd = new SqlCommand();
   PrepareCommand(trans.Connection, trans,cmd, cmdType, cmdText, cmdParms);
   int val = cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   return val;
  }

  public static object ExecuteScalar(CommandType cmdType,string cmdText,params SqlParameter[] cmdParams)
  {
   SqlConnection conn = SQLHelper.GetConn();
   SqlCommand cmd = new SqlCommand();
   PrepareCommand(conn,null,cmd,cmdType,cmdText,cmdParams);
   object val;
   try
   {
    val= cmd.ExecuteScalar();
   }
   catch
   {
    throw;
   }
   finally
   {
    cmd.Parameters.Clear();
    CloseConn(conn);
   }
   return val;
  }

  public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
  {
   SqlCommand cmd = new SqlCommand();
   PrepareCommand(trans.Connection, trans, cmd, cmdType, cmdText, cmdParams);
   object val = cmd.ExecuteScalar();
   cmd.Parameters.Clear();
   return val;
  }

  
  public static SqlDataReader ExecuteReader(CommandType cmdType,string cmdText,params SqlParameter[] cmdParams)
  {
   SqlConnection conn = SQLHelper.GetConn();
   SqlCommand cmd = new SqlCommand();
   try
   {
    PrepareCommand(conn,null,cmd,cmdType,cmdText,cmdParams);
    SqlDataReader rdr=cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    cmd.Parameters.Clear();
    return rdr;
   }
   catch(Exception ex )
   {
    CloseConn(conn);
    throw new Exception(ex.Message);
   }
  }

  public static DataView ExecuteDataView(CommandType cmdType,string cmdText,params SqlParameter[] cmdParams)
  {
   SqlConnection conn = SQLHelper.GetConn();
   SqlCommand cmd = new SqlCommand();
   try
   {
    PrepareCommand(conn,null,cmd,cmdType,cmdText,cmdParams);
    SqlDataAdapter dap = new SqlDataAdapter();
    dap.SelectCommand = cmd;
    DataSet ds = new DataSet();
    dap.Fill(ds,"DefaultTable");
    DataView dv = new DataView();
    dv = ds.Tables["DefaultTable"].DefaultView;
    return dv;
   }
   catch
   {
    throw;
   }
   finally
   {
    CloseConn(conn);
   }
  }
  
  
  public static DataSet ExecuteDataSet(string strTableName,CommandType cmdType,string cmdText,params SqlParameter[] cmdParams)
  {
   SqlConnection conn = SQLHelper.GetConn();
   SqlCommand cmd = new SqlCommand();
   try
   {
    PrepareCommand(conn,null,cmd,cmdType,cmdText,cmdParams);
    SqlDataAdapter dap = new SqlDataAdapter();
    dap.SelectCommand = cmd;
    DataSet ds = new DataSet();
    dap.Fill(ds,strTableName);
    cmd.Parameters.Clear();
    dap.Dispose();
    cmd.Dispose();    
    return ds;
   }
   catch
   {
    throw;
   }
   finally
   {
    CloseConn(conn);
   }
  }

  public static void PrepareCommand(SqlConnection conn,SqlTransaction trans,SqlCommand cmd,CommandType cmdType,string cmdText,SqlParameter[] cmdParas)
  {
   cmd.Connection=conn;
   cmd.CommandText=cmdText;
   cmd.CommandType=cmdType;
   if (trans != null)
   {
    cmd.Transaction = trans;
   }
   if(cmdParas != null)
   {
    foreach(SqlParameter parm in cmdParas)
    {
     if(parm != null)
     {
      cmd.Parameters.Add(parm);
     }
    }
   }
  }

  public static DataSet Query(string SQLString)
  {
   using (SqlConnection conn = SQLHelper.GetConn())
   {
    DataSet ds = new DataSet();
    try
    {
     SqlDataAdapter command = new SqlDataAdapter(SQLString,conn);    
     command.Fill(ds,"ds");
    }
    catch(System.Data.SqlClient.SqlException ex)
    {    
     throw new Exception(ex.Message);
    }     
    finally
    {
     CloseConn(conn);
    }
    return ds;
   }   
  }
 }
}

你可能感兴趣的:(.net)