数据库访问层设计
在应用程序WebVote中添加访问投票表Votes的类Vote,该类封装对投票项目表Votes中记录的选择,添加,修改和删除的方法。其中:
方法GetVotes() 从投票项目表Votes中获取所用投票项目的信息;
方法AddVote(String sItem) 添加一条新记录到投票项目表Votes 中;
方法UpdateVote(int nVoteID) 更新参与投票项目的票数;
方法DeleteVote(int nVoteID) 从投票项目表 Votes中获取删除一条投票项目信息。
类 Vote 的程序设计代码如下:
using
System;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
///<summary>
///class1的摘要说明
///</summary>
public
class
Vote
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
publicSqlDataReaderGetVotes()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//定义类SQLHelper
SQLHelper.SQLHelpersqlHelper=newSQLHelper.SQLHelper();
//定义保存从数据库获取的结果的DataReader
sqlDataReaderdr=null;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//执行存储过程
sqlHelper.RunProc("Pr_GetVotes",outdr);
}
catch(Exceptionex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//抛出执行数据库异常
systemError.CreateErrorLog(ex.Message);
thrownewException(ex.Message,ex);
}
//返回从数据库获取的结果
return(dr);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
publicintAddVote(StringsItem)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//定义类SQLHelper
SQLHelper.SQLHelpersqlHelper=newSQLHelper.SQLHelper();
//创建访问数据库的参数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
SqlParameter[]paramList=...{
sqlHelper.CreateInParam("@Item",SqlDbType.VarChar,100,sItem)
};
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//执行存储过程
return(sqlHelper.RunProc("Pr_AddVote",paramList));
}
catch(Exceptionex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//抛出执行数据库异常
SystemError.CreateErrorLog(ex.Message);
thrownewException(ex.Message,ex);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
publicvoidUpdateVote(intnVoteID)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//定义类SQLHelper
SQLHelper.SQLHelpersqlHelper=newSQLHellper.SQLHelper();
//创建访问数据库的参数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
SqlParameter[]paramList=...{
sqlHelper.CreateInParam("@VoteID",SqlDbType.Int,4,nVoteID)
};
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//执行存储过程
sqlHelper.RunProc("Pr_UpdateVote",paramList);
}
catch(Exceptionex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//抛出执行数据库异常
SystemError.CreateErrorLog(ex.Message);
thrownewException(ex.Message.ex);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
publicvoidDeleteVote(intnVoteID)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//定义类SQLHelper
SQLHelper.SQLHelpersqlHelper=newSQLHelper.SQLHelper();
//创建访问数据库的参数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
SqlParameter[]paramList=...{
sqlHelper.CreateInparam("@VoteID",SqlDbType.Int,4,nVoteID)
};
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//执行存储过程
sqlHelper.RunProc("Pr_DeleteVote",paramList);
}
catch(Exceptionex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{//抛出执行数据库异常
SystemError.CreateErrorLog(ex.Message);
thrownewException(ex.Message,ex);
}
}
}