`
yzd
  • 浏览: 1813161 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Enterprise Library 2.0 -- Data Access Application Block

 
阅读更多
<p>前面介绍了Enterprise Library 2.0 中的Caching Application Block 和 Cryptography  Application Block,今天我们来看一下Enterprise Library 2.0里对数据处理的部分--Data Access  Application  Block。它为我们开发者提供了对数据处理的大部分通用的方法。其内容包括:<br>1、使用DataReader返回多行数据<br>2、使用DataSet返回多行数据<br>3、返回一个输出型参数<br>4、返回一个单一值<br>5、事务处理<br>6、返回XML数据<br>7、使用DataSet来修改数据库<br>8、<a href="http://pw.cnblogs.com/archive/2006/06/12/424131.html" id="_2bf7c5412f1d_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl00_TitleUrl" class="postTitle2"><font color="#1a8bc8">Enterprise Library 2.0 -- Data Access Application Block  (补充)</font></a> <br>...<br>看起来DataAccess Application  Block很像是ADO.NET,确实如此,DataAccess Application Block  不是ADO.NET的替代品,它是对ADO.NET的补充。DataAccess Application  Block的优点在于它可以使用相同的代码去访问不同类型的数据库,比如我们的应用程序从Sql Server上转到Oracle,那么使用DataAccess  Application Block 可以让你不需要修改任何代码而达到上述功能,不过配置文件是当然要修改的。当然也不是在任何情况下都要用DataAccess  Application Block,例如,当我们的代码是为了一种很特殊的数据库而编写的话,这时候应该选用ADO.NET。</p>
<p> 首先我们来看一下DataAccess Application Block 是如何配置的(以Sql Server  2000为例)。<br>首先打开Enterprise Library提供的配置工具,选择File--&gt;Open  Application,选中我们项目种的Web.Config或App.Config文件:<br><img border="0" src="/images/cnblogs_com/pw/611.gif" alt=""><br><br>修改DataBase名称为我们需要连接的数据库名称:<br><img border="0" src="/images/cnblogs_com/pw/612.gif" alt=""><br><br>修改Server,指定数据库服务器名:<br><img border="0" src="/images/cnblogs_com/pw/613.gif" alt=""><br><br>再添加两个参数,分别是uid和pwd:<br><img border="0" src="/images/cnblogs_com/pw/614.gif" alt=""><br><br>最后点 File--&gt;Save  All,这样就完成了一个数据库的配置。生成的配置文件内容如下:<br><br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xmlversion="1.0"encoding="utf-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">configuration</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">configSections</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">section</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="dataConfiguration"</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,Microsoft.Practices.EnterpriseLibrary.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">configSections</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">dataConfiguration</span><span style="color: rgb(255, 0, 0);">defaultDatabase</span><span style="color: rgb(0, 0, 255);">="ConnectionString"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">connectionStrings</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="ConnectionString"</span><span style="color: rgb(255, 0, 0);">connectionString</span><span style="color: rgb(0, 0, 255);">="Database=EnterpriseLibrary;Server=shy;IntegratedSecurity=SSPI;uid=sa;pwd=;"</span><span style="color: rgb(255, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt="">providerName</span><span style="color: rgb(0, 0, 255);">="System.Data.SqlClient"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">connectionStrings</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">configuration</span><span style="color: rgb(0, 0, 255);">&gt;</span>
</div>
<p>在其中的ConnectionStrings节里我们可以看到我们刚刚配置时输入的数据库信息。<br><br>  下面我们重点来说一下DataAccess Application Block操作数据库的方法。<br>1、创建一个数据库实例DataBase<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个默认的数据库实例</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">Databasedefaultdb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个名为ConnectionString的数据库实例</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ConnectionString</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">上面两种创建数据库实例的方法的数据库可以是任何类型的数据库,取决于Config文件中的配置信息<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">下面的这种方面展示了创建一个SQL数据库的实例,需引用Microsoft.Practices.EnterpriseLibrary.Data.Sql程序集</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">SqlDatabasedbsql</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ConnectionString</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);">SqlDatabase;<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">我们同样也可以不通过配置文件来创建数据库实例,如下</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">connString</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">server=.;database=EnterpriseLibrary;uid=sa;pwd=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt="">SqlDatabasenocofigdb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">SqlDatabase(connString);</span>
</div>
<p>2、创建DbCommand<br>  DbCommand的创建方式有两种:一种是为了调用存储过程创建,一种是为了执行一般的SQL语句而创建,具体用哪一种方式,就要看你需要执行的是存储过程还是SQL语句了。<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个SQL语句的DbCommand</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">sql</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Select*fromperson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt="">DbCommandSqldbcomm</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(sql);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个存储过程的DbCommand<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">存储过程名称为GetAllPersonByName</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">procName</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">GetAllPersonByName</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt="">DbCommandProcdbcomm</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetStoredProcCommand(procName);</span>
</div>
<p>3、存储过程中的参数处理<br>DataBase类中包含多种传递存储过程参数的方法,也包含了得到和设置参数值的方法,如下:<br>AddParameter.  为存储过程传递一个参数(输入型或输出型) <br>AddInParameter. 为存储过程传递一个输入型参数<br>AddOutParameter.  为存储过程传递一个输出型参数<br>GetParameterValue. 获取某存储过程指定参数的值 <br>SetParameterValue.  为存储过程的某个参数赋值,当你需要用同一个方法来执行插入多条记录时,就可参数给参数赋值的方法来进行</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个存储过程的DbCommand<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">存储过程名称为GetAllPersonByName</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">procName</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">GetAllPersonByName</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt="">DbCommandProcdbcomm</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetStoredProcCommand(procName);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">添加一个输入型的参数</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">db.AddInParameter(Procdbcomm,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">@sex</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.String);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">添加一个输出型的参数</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">db.AddOutParameter(Procdbcomm,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">@name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.String,</span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置参数的值</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">db.SetParameterValue(Procdbcomm,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">@sex</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">男</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">执行存储过程</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);">db.ExecuteNonQuery(Procdbcomm);<br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">得到输出参数的值,注意转化返回值类型</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">outvalue</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">)db.GetParameterValue(Procdbcomm,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">@name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);</span>
</div>
<p>4、执行ExecuteReader方法返回一个IDataReader类型的数据集</p>
<p>  因为ExecuteReader方法在一开始执行时就打开了一个与数据库的连接,所以我们必须注意在使用结束时关闭连接,而用using(){}语句块就能确保ExecuteReader方法在执行完毕时关闭与数据库的连接。<br>  什么时候使用此方法:<br>返回的数据仅仅用来显示,并不对其进行修改,删除等操作;<br>绑定返回的数据到WebForm  Control<br>不需要缓存返回的数据,用完就释放<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">(IDataReaderreader</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.ExecuteReader(CommandType.Text,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select*fromperson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">))<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_105_204_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_105_204_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_105_204_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_105_204_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DataGrid1.DataSource</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">reader;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DataGrid1.DataBind();<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>5、执行ExecuteDataSet方法返回一个DataSet<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">DataSetGetPersonList()<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_39_235_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_39_235_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_39_235_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_39_235_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbcomm</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select*fromperson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">db.ExecuteDataSet(dbcomm);<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>什么时候使用此方法:<br> 需要返回多表数据;<br>  如果你需要逐一访问数据库的每一条记录,这个时候如果使用DataReader的话,则会使与数据库的连接一直处于打开的状态,长此以往,就会使应用程序的性能和可伸缩性大大降低;<br>  需要和其他的应用程序交换数据;<br><br>6、执行ExecuteNonQuery<br>  该方法返回的是SQL语句执行影响的行数,我们可以利用该方法来执行一些没有返回值的操作(Insert,Update,Delete)<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">AddPerson()<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_33_250_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_33_250_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_33_250_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_33_250_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbcomm</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">insertintopersonvalues(1,'shy','女','123456')</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.ExecuteNonQuery(dbcomm);<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>7、执行ExecuteScalar返回单值<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">GetPersonName()<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_39_245_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_39_245_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_39_245_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_39_245_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbcomm</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">selectnamefromperson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">)db.ExecuteScalar(dbcomm);<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>8、事务处理<br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">UseTransaction()<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_39_932_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_39_932_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_39_932_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_39_932_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbcomm1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">updatepersonsetname='pw'</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbcomm2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">deletefrompersonwhereid=1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">(DbConnectionconn</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.CreateConnection())<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_352_922_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_352_922_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_352_922_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_352_922_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">打开连接</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">conn.Open();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建事务</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">DbTransactiontrans</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">conn.BeginTransaction();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_529_716_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_529_716_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_529_716_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_529_716_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.ExecuteNonQuery(dbcomm1);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.ExecuteNonQuery(dbcomm2);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">都执行成功则提交事务</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">trans.Commit();<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception)<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_767_855_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_767_855_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_767_855_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_767_855_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">发生异常,事务回滚</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">trans.Rollback();<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">关闭连接</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">conn.Close();<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>9、执行ExecuteXmlReader返回XML数据<br> 支持Sql Server 2000  及以后版本的数据库,对微软以外的数据库应该不支持。<br><br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">UseXMLReader()<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_36_1290_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_36_1290_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_36_1290_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_36_1290_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">SqlDatabasedbSQL</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ConnectionString</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);">SqlDatabase;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">要返回XML数据需要在SQL语句后加FORXMLAUTO</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">sqlCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SELECTProductID,ProductNameFROMProductsFORXMLAUTO</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">dbSQL.GetSqlStringCommand(sqlCommand);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">XmlReaderpersonReader</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">StringBuilderpersonlist</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">StringBuilder();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_484_916_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_484_916_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_484_916_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_484_916_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">personReader</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">dbSQL.ExecuteXmlReader(dbCommand);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">循环向XML中写入我们查询得到的数据</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">personReader.EOF)<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_649_902_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_649_902_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_649_902_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_649_902_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(personReader.IsStartElement())<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_726_884_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_726_884_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_726_884_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_726_884_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">personlist.Append(personReader.ReadOuterXml());<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">personlist.Append(Environment.NewLine);<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_950_1278_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_950_1278_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_950_1278_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_950_1278_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">关闭Reader.</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(personReader</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_1040_1100_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_1040_1100_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_1040_1100_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_1040_1100_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">personReader.Close();<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">关闭数据库连接</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(dbCommand.Connection</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_1196_1264_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_1196_1264_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_1196_1264_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_1196_1264_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">dbCommand.Connection.Close();<br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>10、用DataSet批量的添加,修改,删除数据<br><br></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">UpdateDataBase()<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_37_1912_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_37_1912_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_37_1912_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_37_1912_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Databasedb</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">DatabaseFactory.CreateDatabase();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DataSetpersonDataSet</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">DataSet();<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">sqlCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Select*fromperson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddbCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetSqlStringCommand(sqlCommand);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">personTable</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">person</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">得到初始化数据</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">db.LoadDataSet(dbCommand,personDataSet,personTable);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">得到未修改前的数据集</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">DataTabletable</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">personDataSet.Tables[personTable];<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">往DataSet中添加一行数据</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_596_630_Open_Image" alt=""><img align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" id="Codehighlighter1_596_630_Closed_Image" alt=""></span><span style="color: rgb(0, 0, 0);">DataRowaddedRow</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">table.Rows.Add(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">object</span><span style="color: rgb(0, 0, 0);">[]</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_596_630_Closed_Text"><img src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_596_630_Open_Text"><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(0, 0, 0);">18</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Newperson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">男</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">654321</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">}</span></span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">修改</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">table.Rows[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ProductName</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Modifiedproduct</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">下面分别创建添加,修改,删除的操作</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);">DbCommandinsertCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetStoredProcCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">AddPerson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(insertCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.String,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(insertCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Sex</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.String,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Sex</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(insertCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.Int32,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommanddeleteCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetStoredProcCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">DeletePerson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(deleteCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.Int32,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">DbCommandupdateCommand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.GetStoredProcCommand(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">UpdatePerson</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(updateCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.String,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(updateCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Sex</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.String,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Sex</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">db.AddInParameter(insertCommand,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DbType.Int32,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,DataRowVersion.Current);<br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">提交对DataSet的修改,并返回影响的行数</span><span style="color: rgb(0, 128, 0);"><br><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">rowsAffected</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db.UpdateDataSet(productsDataSet,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Products</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,insertCommand,updateCommand,deleteCommand,Microsoft.Practices.EnterpriseLibrary.Data.UpdateBehavior.Standard);<br><img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span>
</div>
<p>关于DataAccess Application Block  的配置以及使用先介绍到此,更深入的部分,我会在以后的文章里继续写的,也许你想知道2.0和1.0的区别,那么请参考我前面写的一篇1.0的数据访问程序块的介绍吧!</p>
<p><a href="http://pw.cnblogs.com/archive/2006/05/30/412599.html" id="_18d3eca7fb27_HomePageDays_DaysList_ctl03_DayItem_DayList_ctl00_TitleUrl" class="postTitle2">Enterprise  Library1.0 -- DataAccess Application Block</a> </p>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics