可执行个性化存储过程的数据访问层(DAL) (C#实现)

        本文意在解释如何从应用层执行单条或者批量的存储过程(主要调用没有任何结果集返回的存储储过程,当然也可以拓展到调用有结果集返回的存储过程).

数据访问层:
  本层主要包括有下列成员:
  • ParamData 结构
  • StoredProcedure 类
  • StoredProcedureCollection 类
  • Execute 类

其中ParamData 含有存储过程参数的名称,值,以及各自的数据类型.

 1 None.gif struct  ParamData
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gifpublic string pName,pValue;
 4InBlock.gifpublic SqlDbType pDataType;
 5InBlock.gifpublic ParamData(string pName,SqlDbType pDataType,string pValue)
 6ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 7InBlock.gifthis.pName=pName;
 8InBlock.gifthis.pDataType=pDataType;
 9InBlock.gifthis.pValue=pValue;
10ExpandedSubBlockEnd.gif}

11ExpandedBlockEnd.gif}

12 None.gif

StoredProcedure 含有SetParam 和 Getparam两大方法,他们分别用来设置和获取存储过程的参数列表的.

None.gif public   void  SetParam( string  pName,SqlDbType pDataType, string  pValue)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifParamData pData
=new ParamData(pName,pDataType,pValue);
InBlock.gif
// adding to array list sParams.
InBlock.gif
sParams.Add(pData);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
public  ArrayList GetParams()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if (!(sParams==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return sParams;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return null;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

StoredProcedureCollection类 是一个集合类, 它含有多个Stored Procedure以及Add和Remove,Item方法.

None.gif public   void  add(StoredProcedure value)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifList.Add(value);
ExpandedBlockEnd.gif}

None.gif
public   void  Remove( int  index)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if (index > Count - 1 || index < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifConsole.WriteLine(
"No data to remove");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifList.RemoveAt(index); 
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
public  StoredProcedure Item( int  Index)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
return (StoredProcedure) List[Index];
ExpandedBlockEnd.gif}

Execute 类 包括有一个ExecuteSps 静态方法,它主要执行来自StoredProcedureCollection类的存储过程

public   static   bool  ExecuteSps(StoredProcedureCollection spCollection,SqlConnection Connection)
{
try
{
foreach (StoredProcedure spData  in  spCollection)
{
SqlCommand cmd
= new  SqlCommand();
int  i = 0 ;
if  (Connection.State !=  ConnectionState.Open)
Connection.Open();
cmd.Connection
= Connection; cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText
= spData.ProcName;
IEnumerator myEnumerator 
=  spData.GetParams().GetEnumerator();
while  (myEnumerator.MoveNext())
{
ParamData pData
= (ParamData)myEnumerator.Current;
cmd.Parameters.Add(pData.pName,pData.pDataType);
cmd.Parameters[i].Value
= pData.pValue;
i
= i + 1 ;
}
cmd.ExecuteNonQuery();
}
return   true
}
catch (Exception exc)
{
return   false ;
}
}

更多实现细节,见 DataAccessLayer.ZIP
应用层:
在应用层我们添加对DataAccessLayer.dll的引用后,我们就可以随意地在需要的地方调用数据层的功能.这样做的一大优势就是:我们可以随着存储过程需求的变化,来添加和删除任意多的参数.

 1 None.gif private   void  button1_Click( object  sender, System.EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gifSqlConnection connection=new SqlConnection();
 4InBlock.gif//change this connect string as per your environment
 5InBlock.gifstring connectString="Persist Security Info=False;Integrated Security=SSPI;database=DB1;server=Server2;Connect Timeout=60";
 6InBlock.gifconnection.ConnectionString=connectString;
 7InBlock.gifif (connection.State!=ConnectionState.Open)
 8InBlock.gifconnection.Open();
 9InBlock.gifDataAccessLayer.StoredProcedureCollection spCollection=new DataAccessLayer.StoredProcedureCollection();
10InBlock.gifDataAccessLayer.StoredProcedure spData=new DataAccessLayer.StoredProcedure();
11InBlock.gifspData.ProcName=txtSpName.Text;
12InBlock.gifspData.SetParam(txtParam1.Text,SqlDbType.VarChar,txtParamValue1.Text);
13InBlock.gifspData.SetParam(txtParam2.Text,SqlDbType.VarChar,txtParamValue2.Text);
14InBlock.gifspCollection.add(spData);
15InBlock.gifif (DataAccessLayer.Execute.ExecuteSps(spCollection,connection))
16InBlock.gifMessageBox.Show("Successfully executed");
17ExpandedBlockEnd.gif}
 
18 None.gif}
19 None.gif catch (Exception exc)
20 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
21InBlock.gifreturn false;
22ExpandedBlockEnd.gif}

23 None.gif}
更多实现细节  见 CallingAPP.zip

转载于:https://www.cnblogs.com/finesite/archive/2005/10/23/260280.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值