<p><span style="font-size:24px"><strong>应用说明</strong></span></p>
<p><span style="font-size:14px"> 数据适配器有SelectCommand、InsertCommand、DeleteCommand、UpdateCommand四种命令对象。分别给每种命令对象赋予相应的命令,就可以用数据适配器对数据集进行更新操作了。</span></p>
<p><span style="font-size:14px"> 使用数据适配器进行更新有一个很重要的方法,这里需要特别的说明一下。</span></p>
<p><span style="font-size:14px"><span style="color:#2B91AF">SqlDataAdapter</span><span style="color:#2B91AF">一般和SqlCommandBuilder</span>配合使用,可以省略很多SQL语句的编写工作。</span></p>
<p align="left"><span style="font-size:14px"><span style="color:blue">string</span> source =<span style="color:#A31515">"server=.;integratedsecurity=SSPI;database=wl_client"</span>;</span></p>
<p align="left"><span style="font-size:14px"><span style="color:blue">string</span> select =<span style="color:#A31515">"select * fromWlcStore_StoreInfo where storecode = '001'"</span>;</span></p>
<p align="left"><span style="font-size:14px"><span style="color:#2B91AF">SqlConnection</span> conn =<span style="color:blue">new</span> <span style="color:#2B91AF">SqlConnection</span>(source);</span></p>
<p align="left"><span style="font-size:14px">// 构造适配器的时候提供了一个查询语句,此时会自动初始化适配器的SelectCommand,此时适配器的其他几个命令对象未初始化,此时只能填充数据集,还不能更新数据集。</span></p>
<p align="left"><span style="font-size:14px"><span style="color:#2B91AF">SqlDataAdapter</span> da =<span style="color:blue">new</span> <span style="color:#2B91AF">SqlDataAdapter</span>(select, conn);</span></p>
<p align="left"><span style="font-size:14px"><span style="color:#2B91AF">DataSet</span> ds =<span style="color:blue">new</span> <span style="color:#2B91AF">DataSet</span>();</span></p>
<p align="left"><span style="font-size:14px">da.Fill(ds, <span style="color:#A31515"> "Customers"</span>);</span></p>
<p align="left"><span style="font-size:14px">// 注意此语句, 用适配器去初始化一个SqlCommandBuilder,就这么简单的一个语句,运行库自动为我们完成适配器的三种更新操作的命令对象的初始化工作,运行库自动生成SQL语句,前面提供的查询语句就是运行库生成更新操作SQL语句的依据,所以第一步的查询语句不能少。</span></p>
<p><span style="font-size:14px"><span style="color:#2B91AF">SqlCommandBuilder</span> scb =<span style="color:blue">new</span> <span style="color:#2B91AF">SqlCommandBuilder</span>(da);</span></p>
<p><span style="font-size:14px"> </span></p>
<p><span style="font-size:14px">// 在数据集中新增一行</span></p>
<p align="left"><span style="color:#2B91AF"><span style="font-size:14px">DataRowdr = ds.Tables[0].NewRow();</span></span></p>
<p align="left"><span style="color:#2B91AF"><span style="font-size:14px">dr[0] ="002";</span></span></p>
<p align="left"><span style="font-size:14px"><span style="color:#2B91AF">dr[1] ="</span><span style="color:#2B91AF">新增仓库一";</span></span></p>
<p align="left"><span style="color:#2B91AF"><span style="font-size:14px">ds.Tables[0].Rows.Add(dr);</span></span></p>
<p align="left"><span style="color:#2B91AF"><span style="font-size:14px"> </span></span></p>
<p align="left"><span style="font-size:14px"><span style="color:#2B91AF">// </span> <span style="color:#2B91AF">修改数据集第一行,第一列的值</span></span></p>
<p><span style="color:#2B91AF"><span style="font-size:14px">ds.Tables[0].Rows[0][0]= "00001";</span></span></p>
<p><span style="color:#2B91AF"><span style="font-size:14px"> </span></span></p>
<p><span style="font-size:14px"><span style="color:#2B91AF">// </span><span style="color:#2B91AF">删除数据集中第二列的值</span></span></p>
<p><span style="color:#2B91AF"><span style="font-size:14px">ds.Tables[0].Rows[1].Delete();</span></span></p>
<p><span style="font-size:14px"><span style="color:#2B91AF">// </span><span style="color:#2B91AF">直接一个更新语句,ds</span>中所有的更新都会反映到数据库中。</span></p>
<p><span style="color:#2B91AF"><span style="font-size:14px">da.Update(ds,"Customers");</span></span></p>
<p><span style="font-size:14px"> 下面解释一个这个语句的原理:SqlCommandBuilder为我们自动生成了更新操作语句,包括插入操作,修改操作和删除操作,语句的生成依据是我们提供的查询语句(包含数据表,列信息)。DataRow可以记录行中每一列的版本状态值(初始值,默认值,修改值等)。那么在执行Update这个语句时,运行库会自动查找每一行的主键的初始值,根据这个值在数据库中定位到唯一的一行,然后把这一行的修改保存起来,即使这一行的主键值发生了改变,运行库还是能定位到正确的行上进行修改。</span></p>
<p><span style="font-size:14px"> 从上面的描述,我们可以看到&# |
|