更好的使用SQLHelper类

微软提供的Data Access Application Block中的SQLHelper类中封装了最常用的数据操作,各个使用者调用他而写的代码也有很大区别。

对于一个返回DataSet的方法我原来是这样写的:

public  DataSet GetDepartmentMemberList( int  departmentID)
  
{
   
try
   
{
    
string sql = "OA_Department_GetDepartment_Members";
    
string conn = ConfigurationSettings.AppSettings["strConnection"];
    SqlParameter[] p 
= 
    
{
     SqlHelper.MakeInParam(
"@departmentID",SqlDbType.Int,4,departmentID)
    }
;
    DataSet ds 
= SqlHelper.ExecuteDataset(conn,CommandType.StoredProcedure,sql,p);
    
return ds;
    }

   
catch(System.Data.SqlClient.SqlException er)
   
{
    
throw new Exception(er.Message);
   }

  }

现在我是这样来写的:

// 连接字符串
private   string  _connectionString  =  ConfigurationSettings.AppSettings[ " strConnection " ];
        
public   string  ConnectionString
        
{
            
get {return this._connectionString;}
            
set {this._connectionString = value;}
        }


public  DataSet GetNewsToIndexPage( int  NewsTypeID)
        
{
            
return GetDataSet("yzb_GetNewsToIndexPage",GetNewsToIndexPage_Parameters(NewsTypeID));
        }


// 设置存储过程参数
         private  SqlParameter[] GetNewsToIndexPage_Parameters( int  NewsTypeID)
        
{

            SqlParameter[] p 
= 
            
{
                SqlHelper.MakeInParam(
"@NewsTypeID",SqlDbType.Int,4,NewsTypeID)
            }
;
            
return p;
        }


// 这里才真正调用SqlHelper
private  DataSet GetDataSet( string  sql,  params  SqlParameter[] p)
        
{
            
return SqlHelper.ExecuteDataset(ConnectionString,CommandType.StoredProcedure,sql,p);
        }

代码更加灵活,更加安全了:P

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值