DataAdapter & DataSet 使用小结

论坛 期权论坛     
选择匿名的用户   2021-5-30 01:46   244   0
  
<div style="MARGIN-LEFT: 21pt; TEXT-INDENT: -21pt">
<span style="FONT-SIZE: 9pt">一、</span>
<span style="FONT-SIZE: 9pt"> Data Adapter的主要作用</span>
</div>
<div style="TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt">Adapter</span>
<span style="FONT-SIZE: 9pt">是database与Dataset或DataTable之间的接口,它从数据库中get数据并填充至Dataset或Data table,这样就可以实现离线处理数据的能力。</span>
</div>
<div style="TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt">一旦adapter对象将数据填充或提交完毕,它和所填充的Dataset对象就没有了任何联系。</span>
</div>
<div style="MARGIN-LEFT: 21pt; TEXT-INDENT: -21pt">
<span style="FONT-SIZE: 9pt">二、</span>
<span style="FONT-SIZE: 9pt">Data Adapter</span>
<span style="FONT-SIZE: 9pt">重要属性或方法</span>
</div>
<div style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt">
<span style="FONT-SIZE: 9pt">(1)<span style="FONT: 7pt &#39;Times New Roman&#39;">    </span></span>
<span style="FONT-SIZE: 9pt">Child Commands</span>
<span style="FONT-SIZE: 9pt">:这里主要包括4个commands对象</span>
</div>
<div style="MARGIN-LEFT: 57pt">
<span style="FONT-SIZE: 9pt">SelectCommand</span>
<span style="FONT-SIZE: 9pt">, UpdateCommand, InsertCommand, and DeleteCommand</span>
</div>
<div style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt">
<span style="FONT-SIZE: 9pt">(2)<span style="FONT: 7pt &#39;Times New Roman&#39;">    </span></span>
<span style="FONT-SIZE: 9pt">Table Mappings Collection</span>
</div>
<div style="MARGIN-LEFT: 57pt">
<span style="FONT-SIZE: 9pt">这里需要了解一下填充机制,adapter默认向Dataset填充时,table Name依次是Table/Table1/Table2</span>
<span style="FONT-SIZE: 9pt">…</span>
<span style="FONT-SIZE: 9pt">。我们可以使用Table Mappings属性来设置我们需要的Table Name。一种方法是在fill的时候 指定,这种比较常见,还有一种是使用如下的代码去实现:</span>
</div>
<div align="left" style="MARGIN-LEFT: 42pt; TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt">DbDataAdapter da &#61; factory.CreateDataAdapter();</span>
</div>
<div align="left">
<span style="FONT-SIZE: 9pt">             da.SelectCommand &#61; cmd;</span>
</div>
<div align="left">
<span style="FONT-SIZE: 9pt">             DataTableMapping tableMapping &#61; da.TableMappings.Add(<span style="COLOR: maroon">&#34;Table&#34;</span>, <span style="COLOR: maroon">&#34;OrdersMapping&#34;</span>);</span>
</div>
<div align="left">
<span style="FONT-SIZE: 9pt">             DataColumnMapping colMapping;</span>
</div>
<div align="left">
<span style="FONT-SIZE: 9pt">             colMapping &#61; tableMapping.ColumnMappings.Add(<span style="COLOR: maroon">&#34;OrderID&#34;</span>, <span style="COLOR: maroon">&#34;</span></span>
<span style="FONT-SIZE: 9pt; COLOR: maroon">订单号&#34;</span>
<span style="FONT-SIZE: 9pt">);</span>
</div>
<div style="MARGIN-LEFT: 42pt; TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt">colMapping &#61; tableMapping.ColumnMappings.Add(<span style="COLOR: maroon">&#34;CustomerID&#34;</span>, <span style="COLOR: maroon">&#34;</span></span>
<span style="FONT-SIZE: 9pt; COLOR: maroon">客户名&#34;</span>
<span style="FONT-SIZE: 9pt">);</span>
</div>
<div style="MARGIN-LEFT: 42pt; TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt">再向dataset填充的时候,表名就是 <span style="COLOR: maroon">OrdersMapping</span><span style="COLOR: maroon">,OrderID也被映射成订单号。这种应用在绑定Datagridview的时候可以应用。</span></span>
</div>
<div style="MARGIN-LEFT: 42pt; TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt; COLOR: maroon">使用这个功能时还要注意设置</span>
<span style="FONT-SIZE: 9pt">MissingMappingAction</span>
<span style="FONT-SIZE: 9pt">属性,不同的枚举值代表不同的处理方式。具体如下:</span>
</div>
<div style="MARGIN-LEFT: 42pt; TEXT-INDENT: 21pt">
<span style="FONT-SIZE: 9pt">By default, this property is set to Passthrough. When the MissingMappingAction property is set to this value, the DataAdapter maps missing columns in your results to columns with the same name in your DataSet. Setting this property to Ignore tells the DataAdapter to ignore columns that don’t appear in the mappings collection. You can also set the MissingMappingAction property to Error, which will cause the DataAdapter to throw an exception if it detects a column in the results of your query that does not exist in the mappings collection.</span>
</div>
<div style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt">
<span style="FONT-SIZE: 9pt; COLOR: maroon">(3)<span style="FONT: 7pt &#39;Times New Roman&#39;">    </span></span>
<span style="FONT-SIZE: 9pt; COLOR: maroon">使用data adapter的page功能</span>
</div>
<div style="MARGIN-LEFT: 57pt">
<span style="FONT-SIZE: 9pt">DataAdapter.Fill(DataSet, 0, 20, &#34;Products&#34;)</span>
</div>
<div style="MARGIN-LEFT: 57pt">
<span style="FONT-SIZE: 9pt">注意:这时候即使查询出的记录有很多,也只有</span>
<span style="FONT-SIZE: 9pt">20</span>
<span style="FONT-SIZE: 9pt">条记录被</span>
<span style="FONT-SIZE: 9pt">get</span>
<span style="FONT-SIZE: 9pt">到本地。</span>
</div>
<div style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt">
<sp
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP