﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-ChengKing-文章分类-ADO.Net</title><link>http://www.cnitblog.com/ChengKing/category/1238.html</link><description>ChengKing</description><language>zh-cn</language><lastBuildDate>Mon, 03 Oct 2011 06:33:57 GMT</lastBuildDate><pubDate>Mon, 03 Oct 2011 06:33:57 GMT</pubDate><ttl>60</ttl><item><title>数据库连接字符串集合</title><link>http://www.cnitblog.com/ChengKing/articles/4007.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:43:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4007.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4007.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4007.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4007.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4007.html</trackback:ping><description><![CDATA[(一)常用连接:<BR>1.使用SqlConnection对象:<BR>public void SqlConnectionOpen()<BR>{<BR>&nbsp;&nbsp; SqlConnection conn= new SqlConnection();<BR>&nbsp;&nbsp; conn.ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20";<BR>&nbsp;&nbsp; conn.Open();<BR>}<BR>2.使用OleDbConnection对象:<BR>public void OleDBConnectionOpen()<BR>{<BR>&nbsp;&nbsp; OleDBConnection conn = new OleDbconnection();<BR>&nbsp;&nbsp; conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Customer.mdb";<BR>&nbsp;&nbsp; conn.Open();<BR>}<BR>(二)其它:<BR>1.ODBC连接Access本地数据库<BR>&nbsp; conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:\a.mdb;"+<BR>&nbsp; "Uid=Admin;"+"Pwd=;");<BR>2.ODBC连接Access系统数据库<BR>&nbsp; conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:\a.mdb;"+<BR>&nbsp; "SystemDB=Admin;"+"Pwd=;");<BR>3.ODBC连接Access系统数据库<BR>&nbsp; conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=\\server\share\a.mdb;");<BR>4.ODBC连接Excel系统数据库<BR>&nbsp; conGoodDay.Open("Driver={Microsoft Access Driver(*.xls)};"+"DriverId=790;"+<BR>&nbsp; "Dbq=C:\a.xls;"+"DefaultDir=c:\somepath;");<BR>5.ODBC连接Oracle系统数据库<BR>&nbsp; conGoodDay.Open("Driver={Microsoft ODBC for Oracle};"+"Server=OracleServer.world;"+<BR>&nbsp; "Uid=Admin;"+"Pwd=password;");<BR>6.ODBC连接Sql Servr<BR>&nbsp; conGoodDay.Open("Driver={Sql Server};"+"Server=myServer;"+"Database=myDatabaseName;"<BR>&nbsp; "Uid=Admin;"+"Pwd=password;");<BR>7.ODBC连接Visual FoxPro<BR>&nbsp; conGoodDay.Open("Driver={Microsoft Visual FoxPro Driver};"+<BR>&nbsp; "SourceType=DBC;"+"SourceDB=c:a.dbc;"+"Exclusive=No;");<BR>&nbsp;<img src ="http://www.cnitblog.com/ChengKing/aggbug/4007.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:43 <a href="http://www.cnitblog.com/ChengKing/articles/4007.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ADO.Net都有哪些从数据库获取数据的方式 </title><link>http://www.cnitblog.com/ChengKing/articles/3990.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:22:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3990.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3990.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3990.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3990.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3990.html</trackback:ping><description><![CDATA[<P>(一).说明</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 初学ADO.Net会被ADO.Net访问数据库方式弄得头晕,感觉无从下手.<BR>本文将它们整合在一起,文章并没有涉及太多深层知识. 只是我觉得将它们集中在一起,<BR>在比较中学习更快,更清晰一些: <BR>本文列举了用ADO.Net访问数据库的所有方式:<BR>1.用Sql语句访问数据库<BR>2.用无参存储过程访问数据库<BR>3.用有参存储过程访问数据库<BR>4.使用 CommandBuilder.DeriveParameters 来填充 Command 对象的 Parameters 集合,来访问数据库<BR>5.使用SqlDataReader对象输出数据.</P>
<P>(二).代码示例</P>
<P>private DataSet GetDataSet(int sign)&nbsp;&nbsp;<BR>&nbsp;&nbsp;{</P>
<P>&nbsp;&nbsp; //*******自定义连接字符串********/<BR>&nbsp;&nbsp;&nbsp;string connectionStr="server=(localhost);uid=sa;pwd=;database=NorthWind";&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;SqlConnection myConnection=new SqlConnection(connectionStr);<BR>&nbsp;&nbsp;&nbsp;DataSet myDataSet=new DataSet();<BR>&nbsp;&nbsp;&nbsp;string sqlProcedureName="myprocedure";&nbsp; //此存储过程必须事先已经定义好了.<BR>&nbsp;&nbsp;&nbsp;if(sign==1)//执行语句.&nbsp; (最常用的情况)</P>
<P>&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;string queryStr="select * from products";&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlDataAdapter myAdapter=new SqlDataAdapter(queryStr,connectionStr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.Fill(myDataSet);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return myDataSet;<BR>&nbsp;&nbsp;&nbsp;}</P>
<P><BR>&nbsp;&nbsp;&nbsp;if(sign==2)//调用SQL无参存储过程,此存储过程返回select查询结果<BR>&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand myCommand=new SqlCommand(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.CommandType=CommandType.StoredProcedure;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlDataAdapter myAdapter=new SqlDataAdapter(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.Fill(myDataSet);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return myDataSet;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;if(sign==3)//调用SQL有参存储过程，此存储过程返回select查询结果，且传递参数<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand myCommand=new SqlCommand(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.CommandType=CommandType.StoredProcedure;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlParameter start=new SqlParameter("@start",SqlDbType.Int,4); //&nbsp;定义第一个参数</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;start.Value=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.Parameters.Add(start);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;SqlParameter end=new SqlParameter("@end",SqlDbType.Int,4); // 定义第二个参数</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;end.Value=5;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.Parameters.Add(end);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlDataAdapter myAdapter=new SqlDataAdapter(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.SelectCommand=myCommand;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.Fill(myDataSet);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return myDataSet;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;if(sign==4)&nbsp; //演示如何使用 CommandBuilder.DeriveParameters 来填充 Command 对象的 Parameters 集合<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand myCommand=new SqlCommand(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.CommandType=CommandType.StoredProcedure;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlDataAdapter myAdapter=new SqlDataAdapter(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();&nbsp; //如果不打开连接，下一句将不知道从哪里去取所需的存储过程<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommandBuilder.DeriveParameters(myCommand);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.Parameters["@start"].Value=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.Parameters["@end"].Value=5;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.SelectCommand=myCommand;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.Fill(myDataSet);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return myDataSet;&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;if(sign==5)//练习使用SqlDataReader,结果在控制台输出<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand myCommand=new SqlCommand(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.CommandType=CommandType.StoredProcedure;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlParameter start=new SqlParameter("@start",SqlDbType.Int,4); // 把4改变或去掉<BR>&nbsp;&nbsp;&nbsp;&nbsp;start.Value=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.Parameters.Add(start);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;SqlParameter end=new SqlParameter("@end",SqlDbType.Int,4); // 把4改变或去掉<BR>&nbsp;&nbsp;&nbsp;&nbsp;end.Value=5;<BR>&nbsp;&nbsp;&nbsp;&nbsp;myCommand.Parameters.Add(end);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlDataAdapter myAdapter=new SqlDataAdapter(sqlProcedureName,myConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;myAdapter.SelectCommand=myCommand;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<BR>&nbsp;&nbsp;&nbsp;&nbsp;SqlDataReader myReader = myCommand.ExecuteReader();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1));</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;while (myReader.Read())<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Console.WriteLine("{0},{1}", myReader.GetString(0), myReader.GetString(1));&nbsp; //？不对<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("{0},{1}", myReader.GetValue(0), myReader.GetValue(1));<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;DataTable myTable=new DataTable();<BR>&nbsp;&nbsp;&nbsp;&nbsp;myDataSet.Tables.Add(myTable);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return myDataSet;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new DataSet();<BR>&nbsp;&nbsp;}</P>
<P></P>
<P>&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3990.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:22 <a href="http://www.cnitblog.com/ChengKing/articles/3990.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>当DataSet中包含主/子表时,Update更新步骤</title><link>http://www.cnitblog.com/ChengKing/articles/3986.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:17:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3986.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3986.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3986.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3986.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3986.html</trackback:ping><description><![CDATA[<P>(一) 序幕</P>
<P>当DataSet中同时包含主/子表(主键表/外键表)时,有时候关系约束太严格:<BR>&nbsp;&nbsp; 比如, 关系数据库完整性规则:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.实体完整性. 主键表中主键不准为空.&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.参照完整性. 外键表中外键的值必须与主键表中的主键对应.&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要么为空,要么为主键表中的一主键值. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.自定义完整性.&nbsp;&nbsp;&nbsp; <BR>&nbsp; 如果DataSet表中定义的多表关系约束太严密,直接用Update方法<BR>&nbsp; 将DataSet中多表一起提交时,有可能不满足完整性规则,会发生错误,<BR>&nbsp;&nbsp;&nbsp; 原因: 举个例子,设为两个表: CompanyMain(公司主表)和CompanySon(公司子表)<BR>&nbsp;&nbsp; CompanyMain(公司主表)主要存储一个公司的基本信息,CompanySon(公司子表)<BR>&nbsp;&nbsp; 主要存储此公司的一些客户的信息. 并且公司主表中的(ID)与子表中的(BelongID)<BR>&nbsp;&nbsp; 建立关联,即主外键关系; 一个公司对应着多个客户,即ID:BelongID = 1 : n关系.<BR>&nbsp;&nbsp; 主表的ID为自动生成编号.<BR>&nbsp;&nbsp; 那么: <BR>&nbsp;&nbsp; 在新增公司界面,一起提交时,系统是不会遵守"数据库完整性规则"来更新到数据库的.<BR>&nbsp;&nbsp; 如果它先更新子表,再更新主表有可能就会报错.&nbsp; 原因是主表的公司信息还没有插入<BR>&nbsp; 到数据库中的表,也就没有生成公司编号: ID,则子表更新时就会没有对应的BelongID.<BR>&nbsp;&nbsp; 这时如果设置了: "外键表中的外键不为null"完整性规则,就会引发异常.<BR>&nbsp;&nbsp; 这只是一种出错的可能,还有更多的出错可能性,在分布式设计中更容易出现这样的错误.</P>
<P>(二). 解决办法</P>
<P>&nbsp;&nbsp; 一般遵守以下几条规则,就会避免大量的出错机率. </P>
<P>&nbsp;&nbsp; 1规则.&nbsp;&nbsp; 在DataSet更新之前按 &lt;表&gt; 和 &lt;表的RowState属性&gt;进行拆分提交</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I.&nbsp; 按 &lt;表&gt; 进行拆分提交意思是说: </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对DataSet 中的表不是一起提交,而是一次提交一个表,进行多次提交</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; II.按 &lt;表的RowState属性&gt;进行拆分提交意思是说:</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对DataSet中的单个表根据RowState属性,再进行拆分,对RowState</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 相同的进行一次提交,也是提交多次</P>
<P>&nbsp;&nbsp; 2.规则.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在规则1基础上, 先更新状态为&lt;新增&gt; 和 &lt;修改&gt; 的,再更新&lt;删除的&gt;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 即: 先更新DataRowState 值为: Added和Modified的,再更新: Deleted的.</P>
<P>&nbsp; 3.规则</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在规则1和规则2基础上,如果DataRowState为Added和Modified,则先更新主表,后更新子表.</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在规则1和规则2基础上,如果DataRowState为Deleted,则先更新子表,后更新主表.</P>
<P>(三) 归纳一下上面三条规则,如下:</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.将DataSet中的表拆分,并根据RowState将各个表记录进行分组并存储在不同的数据集中</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //这里要存储在DataSet中,是因为: Update接受的是DataSet的参数,还有WebService只支持</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //DataSet,对其进行序列化操作</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 代码示例:</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设待更新的数据集为: dsCompany(里面包括两个表,分别为主表和子表,并存储了</P>
<P>数据,待更新)</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //dtCompanyMain存储公司主表信息,dtCompanySon存储公司子表信息</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dtCompanyMail = ds.Tables["dtCompanyMain"].Clone();&nbsp; //分离出主表数据</P>
<P>并存储另一对象</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dtCompanySon&nbsp; = ds.Talbes["dtCompanySon"].Clone();&nbsp; //分离出子表数据</P>
<P>并存储另一对象</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataSet dsAdded = new DataSet();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //存放主表 新增 的数据</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataSet dsMidified = new DataSet();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //存储主表&nbsp; 编辑 的行记录</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataSet dsDeleted = new DataSet();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //存储主表 删除 的行记录</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dsAdded = dtCompanyMain.GetChanges(DataRowState.Added);&nbsp;&nbsp; //取得主表中新增的行</P>
<P>记录集</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dsMidified = dtCompanyMain.GetChanges(DataRowState.Modified);&nbsp; //取得主表中编</P>
<P>辑的行记录集</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dsDeleted = dtCompanyMain.GetChanges(DataRowState.Deleted);&nbsp; //取得主表中删除的</P>
<P>行记录集&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.更新主表DataRowState状态为: Added和Modified的记录.</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter.Update(dsAdded,"dtCompanyMain");&nbsp;&nbsp; //更新添加的记录集到数据库</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter.Update(dsModified,"dtCompanyMain");&nbsp;&nbsp; //更新修改的记录集到数据</P>
<P>库</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.更新子表DataRowState状态为: Added和Modified的记录</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .............&nbsp; //代码省略,跟2主表更新类似</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.更新子表DataRowState状态为: Deleted的记录.</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ............&nbsp;&nbsp; // 代码省略,与下面5类似</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5.更新主表DataRowState状态为: Deleted的记录</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter.Update(dsDeleted,"dtCompanyMain");&nbsp;&nbsp; //更新修改的记录集到数据库</P>
<P></P>
<P><BR>不对的地方请批评指正!</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P> <BR></P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3986.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:17 <a href="http://www.cnitblog.com/ChengKing/articles/3986.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自定义带结构的可序列化数据集DataSet.</title><link>http://www.cnitblog.com/ChengKing/articles/3985.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:16:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3985.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3985.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3985.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3985.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3985.html</trackback:ping><description><![CDATA[<P>(一).说明<BR>&nbsp; 在编程过程中有没有遇到这样的问题:&nbsp; 偶尔因为某种原因，数据库表需要改一字段名称。比如：将: Name改为: PersonName. <BR>&nbsp; 接下来程序员就把所有涉及到的代码打开，找到类似: ds.Table[0].Row[n]["Name"]的语句，修改成: ds.Table[0].Row[n]["Name"].<BR>&nbsp; 其实完全可以不用这么做，只加一个带结构的DataSet就OK了。&nbsp; 修改只修改本类代码文件中的一个属性就OK了，而且除本类代码文件<BR>&nbsp; 之外的应用程式代码根本不用修改.<BR>(二).代码:<BR>&nbsp; <BR>using System;<BR>using System.Data;<BR>using System.Runtime.Serialization;</P>
<P>namespace SchemeDataSet<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// 包含客户信息的自定义的可序列化的数据集（DataSet）</P>
<P>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;[System.ComponentModel.DesignerCategory("Code")]<BR>&nbsp;[SerializableAttribute] <BR>&nbsp;public class Customers : DataSet&nbsp;&nbsp;&nbsp; //继承DataSet，这样可以调用DataSet中的所有方法<BR>&nbsp;{<BR>&nbsp;&nbsp;#region 以下为常量的声明<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;/// &lt;summary&gt;常量定义客户资料表 Customer 的表名称&lt;/summary&gt;<BR>&nbsp;&nbsp;public const string CRMPSCUSTOMERS_TABLE = "Customer";</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;该常量定义客户编号的栏位名&lt;/summary&gt;<BR>&nbsp;&nbsp;public const string CUSNO_FIELD = "CUSNO";</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;该常量定义客户名称的栏位名&lt;/summary&gt;<BR>&nbsp;&nbsp;public const string CUSTOMERNAME_FIELD = "CustomerName";</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;该常量定义客户地址的栏位名&lt;/summary&gt;<BR>&nbsp;&nbsp;public const string ADDRESS_FIELD = "Address";<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;#endregion 以下变量、常量、及对象的声明</P>
<P><BR>&nbsp;&nbsp;#region 以下为方法声明</P>
<P><BR>&nbsp;&nbsp;public Customers()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BuildDataTables();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;///&nbsp;&nbsp;&nbsp;&nbsp; 支持序列化的构造函数</P>
<P>&nbsp;&nbsp;///&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="info"&gt;对象的序列化信息&lt;/param&gt;<BR>&nbsp;&nbsp;///&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="context"&gt;关于被呼叫方法的上下文&lt;/param&gt;<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private Customers(SerializationInfo info, StreamingContext context) : base(info, context) <BR>&nbsp;&nbsp;{}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 创建带结构的表：Customer<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void BuildDataTables()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// 创建CRMPSCustomers的数据表<BR>&nbsp;&nbsp;&nbsp;//</P>
<P>&nbsp;&nbsp;&nbsp;DataTable table = new DataTable(CRMPSCUSTOMERS_TABLE);<BR>&nbsp;&nbsp;&nbsp;DataColumnCollection columns = table.Columns;</P>
<P>&nbsp;&nbsp;&nbsp;//定义结构<BR>&nbsp;&nbsp;&nbsp;columns.Add(CUSNO_FIELD,typeof(System.String));<BR>&nbsp;&nbsp;&nbsp;columns.Add(CUSTOMERNAME_FIELD,typeof(System.String));<BR>&nbsp;&nbsp;&nbsp;columns.Add(ADDRESS_FIELD,typeof(System.String));</P>
<P>&nbsp;&nbsp;&nbsp;this.Tables.Add(table);<BR>&nbsp;&nbsp;&nbsp;Contact contact = new Contact();<BR>&nbsp;&nbsp;&nbsp;this.Merge(contact);&nbsp;&nbsp; //合并<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#endregion 以下为方法声明</P>
<P>&nbsp;}<BR>}</P>
<P>OK.<BR>(三).使用<BR>1.<BR>&nbsp; 当定义了这样一个类后，在程式中应该这样使用：&nbsp; <BR>&nbsp; ds.Table[Customers.CRMPSCUSTOMERS_TABLE].Row[n][Customers.CUSTOMERNAME_FIELD];<BR>&nbsp; 这样就把所有的任务交给本类代码文件了。&nbsp; 当修改数据库时不用修改类似这些语句。 只修改一下结构类相关属性的值即可.<BR>&nbsp;2.<BR>&nbsp;定义此带结构的数据集，还有一种情况用到，就是在有些界面，UI层与数据库交互是单向的，即往数据库添加数据之前<BR>&nbsp;没有读取架构，则DataSet也就没有结构（没有结构不能存储数据），这时这个类就起作用了.<BR><BR>不对的地方请批评指正！</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3985.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:16 <a href="http://www.cnitblog.com/ChengKing/articles/3985.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用SqlCommandBuilder 实现批量更新</title><link>http://www.cnitblog.com/ChengKing/articles/3978.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 03:41:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3978.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3978.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3978.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3978.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3978.html</trackback:ping><description><![CDATA[<P>(一)</P>
<P>&nbsp; 有没有人遇到这种情况，用 SqlDataAdapter.Update(ds)更新时出错?</P>
<P>&nbsp; answer:&nbsp;&nbsp; 一般是这样的，如果用设计器将SqlDataAdapter拖到页面中使用时，不会出现这种情况，因为</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 系统会自动生成SqlDataAdapter的属性命令，比如:&nbsp; .UpdateCommane insertCommand </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; selectCommand等。&nbsp; 但是有些程序员不喜欢用设计器，或者是有些地方没必要拖动</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter这么个庞大物来实现,那么SqlDataAdapter就不会自动生成相关的查询或更新</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 语句了.&nbsp;&nbsp; 所以当执行到SqlDataAdapter.Update(ds)语句时，SqlDataAdapter桥接器不知道更</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 新哪个表.就报错了.&nbsp; </P>
<P>(二)</P>
<P>&nbsp; 解决方法:</P>
<P>&nbsp; 用SqlCommandBuilder 实现批量更新</P>
<P>&nbsp; 1.功能： </P>
<P>&nbsp;&nbsp; 可以实现你对DataSet在UI层做任意操作后，直接丢给这个方法，这个方法就可以自动把你的修改更 新到数 据库中,而没必要每次都更新到</P>
<P>&nbsp;&nbsp; 数据库</P>
<P>&nbsp; 2.使用方法<BR>&nbsp; public DataSetUpdateByDataSet(DataSet ds,string strTblName,string strConnection)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlConnection&nbsp; conn = new SqlConnection(strConnection));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter myAdapter = new SqlDataAdapter();<BR>&nbsp; &nbsp;&nbsp; SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn);&nbsp;&nbsp;&nbsp; <BR>&nbsp; &nbsp;&nbsp; myAdapter.SelectCommand = myCommand;<BR>&nbsp; &nbsp;&nbsp; SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp; try</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; lock(this)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //处理并发情况(分布式情况)</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myAdapter.Update(ds,strTblName);&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;catch(Exception err)<BR>&nbsp;{</P>
<P>&nbsp;&nbsp;&nbsp;conn.Close();&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp; throw new BusinessException(err);<BR>&nbsp;}</P>
<P>&nbsp;&nbsp;&nbsp; return ds;&nbsp;&nbsp;&nbsp; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds<BR><BR>}</P>
<P>或</P>
<P>public DataSet UpdateByDataSet(DataSet ds,string strTblName,string strConnection)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlConnection&nbsp; conn = new SqlConnection(strConnection));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn);&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand );&nbsp;&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myAdapter.InsertCommand = myCommandBuilder .GetInsertCommand();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myAdapter.UpdateCommand = myCommandBuilder .GetUpdateCommand();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; myAdapter.DeleteCommand = myCommandBuilder .GetDeleteCommand();</P>
<P>&nbsp;&nbsp; try</P>
<P>&nbsp; {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lock(this)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;//处理并发情况(分布式情况)</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; conn.Open();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; myAdapter.Update(ds,strTblName);&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Close();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ds;&nbsp;&nbsp;&nbsp; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds&nbsp;}<BR>&nbsp;catch(Exception err)<BR>&nbsp;{&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn.Close();&nbsp;<BR>&nbsp;&nbsp; &nbsp;&nbsp; throw new BusinessException(err);<BR>&nbsp;}<BR>}</P>
<P>直接调用这两个方法的任意一个就OK啦,说明的一点是select * from "+strTblName是一定要的，<BR>作用大家也应该想到了，主要是告诉 SqlDataAdapter更新哪个表</P>
<P><BR>3.什么时候用?</P>
<P>&nbsp;&nbsp;&nbsp; a. 有时候需要缓存的时候，比如说在一个商品选择界面，选择好商品，并且进行编辑/删除/更新后，</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最后一并交给数据库，而不是每一步操作都访问数据库，因为客户选择商品可能进行n次编辑/删除</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更新操作，如果每次都提交，不但容易引起数据库冲突,引发错误，而且当数据量很大时在用户执行</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 效率上也变得有些慢</P>
<P>&nbsp;&nbsp;&nbsp; b.有的界面是这样的有的界面是这样的，需求要求一定用缓存实现，确认之前的操作不提交到库,点击</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 页面专门提交的按钮时才提交商品选择信息和商品的其它信息. 我经常遇到这样的情况</P>
<P>&nbsp;&nbsp;&nbsp; c.有些情况下只往数据库里更新，不读取. 也就是说没有从数据库里读，SqlDataAdapter也就不知道是<BR>&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更新哪张表了，调用Update就很可能出错了。这样的情况下可以用SqlCommandBuilder 了.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>4.<BR>&nbsp;注意点: <BR>&nbsp; 1.只能更新一个表，如果此数据集是从两个或者两个以上的表关联而来的,则不能用此方法自动更新<BR>&nbsp; 2.表中必须设置主键<BR>&nbsp; 3.更新的表中字段不能有image类型的</P>
<P>5.优点:</P>
<P>&nbsp;&nbsp;&nbsp; 节省代码量，节省时间，这个方法可以代替所有的:&nbsp; 更新/删除/插入操作语句</P>
<P>6.缺点:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 访问两次数据库(select * TableName,就是这句，要确认是哪个表，除非是很大的数据量，<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一般是感觉不到的)，效率稍微有些慢<BR></P>
<P><BR>不对的地方望批评指正！</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3978.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 11:41 <a href="http://www.cnitblog.com/ChengKing/articles/3978.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>