金沙官网线上:Asp.net(C#)分层——基础类

trans.Commit();
return result;
}
catch (Exception ex)
{
trans.Rollback();
return 0;
}
}
}

  private static void PrepareCommand(SqlCommand cmd,SqlConnection conn,SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
  {
   if (conn.State != ConnectionState.Open)
    conn.Open();
   cmd.Connection = conn;
   cmd.CommandText = cmdText;
   if (trans != null)
    cmd.Transaction = trans;
   cmd.CommandType = CommandType.Text;//cmdType;
   if (cmdParms != null)
   {
    foreach (SqlParameter parm in cmdParms)
     cmd.Parameters.Add(parm);
   }
  }

/// <summary>
/// 对数据库执行增、删、改命令
/// </summary>
/// <param name="sql">T-Sql语句</param>
/// <param name="pa">参数数组</param>
/// <param name="iCount">成功情况下影响行数</param>
/// <returns>受影响的记录数</returns>
public static int ExecuteNonQuery(string connectionString, string sql, SqlParameter[] pa, int iCount)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
Connection.Open();
SqlTransaction trans = Connection.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Transaction = trans;
if (pa != null)
{
cmd.Parameters.AddRange(pa);
}

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

其中ExecuteNonQuery中的 iCount 参数就是用来进行事物处理的

namespace LiTianPing.SQLServerDAL //可以修改成实际项目的命名空间名称
{
 /// <summary>
 /// Copyright (C) 2004-2008 LiTianPing
 /// 数据访问基础类(基于SQLServer)
 /// 用户可以修改满足自己项目的需要。
 /// </summary>
 public abstract class DbHelperSQL
 {
  //数据库连接字符串(web.config来配置)
  //<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" />  
  protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
  public DbHelperSQL()
  {   
  }
  
  #region  执行简单SQL语句

方法如下:

 }
}

 

  #region 存储过程操作

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
if (ds.Tables != null && ds.Tables.Count > 0)
{
return ds.Tables[0];
}
}
catch (Exception ex)
{
return null;
}
return null;
}
}

  #endregion

个人认为使用率较高也较为方便的是   ExecuteNonQuery (增、删、改),ExecuteDataTable(支持存储过程和SQL语句查询,返回tadatable)

  
  /// <summary>
  /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
  /// </summary>
  /// <param name="connection">数据库连接</param>
  /// <param name="storedProcName">存储过程名</param>
  /// <param name="parameters">存储过程参数</param>
  /// <returns>SqlCommand</returns>
  private static SqlCommand BuildQueryCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)
  {   
   SqlCommand command = new SqlCommand( storedProcName, connection );
   command.CommandType = CommandType.StoredProcedure;
   foreach (SqlParameter parameter in parameters)
   {
    command.Parameters.Add( parameter );
   }
   return command;   
  }
  
  /// <summary>
  /// 执行存储过程,返回影响的行数  
  /// </summary>
  /// <param name="storedProcName">存储过程名</param>
  /// <param name="parameters">存储过程参数</param>
  /// <param name="rowsAffected">影响的行数</param>
  /// <returns></returns>
  public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
  {
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
    int result;
    connection.Open();
    SqlCommand command = BuildIntCommand(connection,storedProcName, parameters );
    rowsAffected = command.ExecuteNonQuery();
    result = (int)command.Parameters["ReturnValue"].Value;
    //Connection.Close();
    return result;
   }
  }
  
  /// <summary>
  /// 创建 SqlCommand 对象实例(用来返回一个整数值) 
  /// </summary>
  /// <param name="storedProcName">存储过程名</param>
  /// <param name="parameters">存储过程参数</param>
  /// <returns>SqlCommand 对象实例</returns>
  private static SqlCommand BuildIntCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)
  {
   SqlCommand command = BuildQueryCommand(connection,storedProcName, parameters );
   command.Parameters.Add( new SqlParameter ( "ReturnValue",
    SqlDbType.Int,4,ParameterDirection.ReturnValue,
    false,0,0,string.Empty,DataRowVersion.Default,null ));
   return command;
  }
  #endregion 

相比较网上通用的SqlHelper类方法主要有一下几点的不同:

       trans.Commit();
      }     
     }
     catch
     {
      trans.Rollback();
      throw;
     }
    }    
   }
  }
 
    
  /// <summary>
  /// 执行一条计算查询结果语句,返回查询结果(object)。
  /// </summary>
  /// <param name="SQLString">计算查询结果语句</param>
  /// <returns>查询结果(object)</returns>
  public static object GetSingle(string SQLString,params SqlParameter[] cmdParms)
  {
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
    using (SqlCommand cmd = new SqlCommand())
    {
     try
     {
      PrepareCommand(cmd, connection, null,SQLString, cmdParms);
      object obj = cmd.ExecuteScalar();
      cmd.Parameters.Clear();
      if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
      {     
       return null;
      }
      else
      {
       return obj;
      }    
     }
     catch(System.Data.SqlClient.SqlException e)
     {    
      throw new Exception(e.Message);
     }     
    }
   }
  }
  
  /// <summary>
  /// 执行查询语句,返回SqlDataReader
  /// </summary>
  /// <param name="strSQL">查询语句</param>
  /// <returns>SqlDataReader</returns>
  public static SqlDataReader ExecuteReader(string SQLString,params SqlParameter[] cmdParms)
  {  
   SqlConnection connection = new SqlConnection(connectionString);
   SqlCommand cmd = new SqlCommand();    
   try
   {
    PrepareCommand(cmd, connection, null,SQLString, cmdParms);
    SqlDataReader myReader = cmd.ExecuteReader();
    cmd.Parameters.Clear();
    return myReader;
   }
   catch(System.Data.SqlClient.SqlException e)
   {        
    throw new Exception(e.Message);
   }     
   
  }  
  
  /// <summary>
  /// 执行查询语句,返回DataSet
  /// </summary>
  /// <param name="SQLString">查询语句</param>
  /// <returns>DataSet</returns>
  public static DataSet Query(string SQLString,params SqlParameter[] cmdParms)
  {
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
    SqlCommand cmd = new SqlCommand();
    PrepareCommand(cmd, connection, null,SQLString, cmdParms);
    using( SqlDataAdapter da = new SqlDataAdapter(cmd) )
    {
     DataSet ds = new DataSet(); 
     try
     {            
      da.Fill(ds,"ds");
      cmd.Parameters.Clear();
     }
     catch(System.Data.SqlClient.SqlException ex)
     {    
      throw new Exception(ex.Message);
     }   
     return ds;
    }    
   }   
  }

本文由金沙官网线上发布于编程,转载请注明出处:金沙官网线上:Asp.net(C#)分层——基础类

您可能还会对下面的文章感兴趣: