---
title: "在线投票系统（三）"
author: "jandroid"
date: "2007-04-05T20:51:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-11-14T09:35:44Z"
id: 1257410
url: "https://jandroid.iteye.com/blog/1257410"
---

**数据库访问层设计**

在应用程序WebVote中添加访问投票表Votes的类Vote,该类封装对投票项目表Votes中记录的选择，添加，修改和删除的方法。其中：

方法GetVotes() 从投票项目表Votes中获取所用投票项目的信息；

方法AddVote(String sItem) 添加一条新记录到投票项目表Votes 中；

方法UpdateVote(int nVoteID) 更新参与投票项目的票数；

方法DeleteVote(int nVoteID) 从投票项目表 Votes中获取删除一条投票项目信息。

类 Vote 的程序设计代码如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///class1的摘要说明  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassVote  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicSqlDataReaderGetVotes()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//定义类SQLHelper  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SQLHelper.SQLHelpersqlHelper=newSQLHelper.SQLHelper();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//定义保存从数据库获取的结果的DataReader  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlDataReaderdr=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//执行存储过程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlHelper.RunProc("Pr\_GetVotes",outdr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(Exceptionex)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//抛出执行数据库异常  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)systemError.CreateErrorLog(ex.Message);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewException(ex.Message,ex);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//返回从数据库获取的结果  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return(dr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicintAddVote(StringsItem)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//定义类SQLHelper  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SQLHelper.SQLHelpersqlHelper=newSQLHelper.SQLHelper();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建访问数据库的参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)SqlParameter\[]paramList=...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlHelper.CreateInParam("@Item",SqlDbType.VarChar,100,sItem)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//执行存储过程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return(sqlHelper.RunProc("Pr\_AddVote",paramList));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(Exceptionex)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//抛出执行数据库异常  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SystemError.CreateErrorLog(ex.Message);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewException(ex.Message,ex);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidUpdateVote(intnVoteID)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//定义类SQLHelper  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SQLHelper.SQLHelpersqlHelper=newSQLHellper.SQLHelper();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建访问数据库的参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)SqlParameter\[]paramList=...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlHelper.CreateInParam("@VoteID",SqlDbType.Int,4,nVoteID)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//执行存储过程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlHelper.RunProc("Pr\_UpdateVote",paramList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(Exceptionex)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//抛出执行数据库异常  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SystemError.CreateErrorLog(ex.Message);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewException(ex.Message.ex);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidDeleteVote(intnVoteID)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//定义类SQLHelper  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SQLHelper.SQLHelpersqlHelper=newSQLHelper.SQLHelper();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建访问数据库的参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)SqlParameter\[]paramList=...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlHelper.CreateInparam("@VoteID",SqlDbType.Int,4,nVoteID)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//执行存储过程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sqlHelper.RunProc("Pr\_DeleteVote",paramList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(Exceptionex)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//抛出执行数据库异常  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SystemError.CreateErrorLog(ex.Message);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewException(ex.Message,ex);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}