关于微软ADO.NET提供的组件库里的UpdateDataSet()的用法心得
来源:互联网 发布:软件qa面试题 编辑:程序博客网 时间:2024/06/05 13:25
函数是这样写的
/// <summary>
/// Executes the respective command for each inserted, updated, or deleted row in the DataSet.
/// </summary>
/// <remarks>
/// e.g.:
/// UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order");
/// </remarks>
/// <param name="insertCommand">A valid transact-SQL statement or stored procedure to insert new records into the data source</param>
/// <param name="deleteCommand">A valid transact-SQL statement or stored procedure to delete records from the data source</param>
/// <param name="updateCommand">A valid transact-SQL statement or stored procedure used to update records in the data source</param>
/// <param name="dataSet">The DataSet used to update the data source</param>
/// <param name="tableName">The DataTable used to update the data source.</param>
public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName)
{
if( insertCommand == null ) throw new ArgumentNullException( "insertCommand" );
if( deleteCommand == null ) throw new ArgumentNullException( "deleteCommand" );
if( updateCommand == null ) throw new ArgumentNullException( "updateCommand" );
if( tableName == null || tableName.Length == 0 ) throw new ArgumentNullException( "tableName" );
// Create a SqlDataAdapter, and dispose of it after we are done
using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
{
// Set the data adapter commands
dataAdapter.UpdateCommand = updateCommand;
dataAdapter.InsertCommand = insertCommand;
dataAdapter.DeleteCommand = deleteCommand;
// Update the dataset changes in the data source
dataAdapter.Update (dataSet, tableName);
// Commit all the changes made to the DataSet
dataSet.AcceptChanges();
}
}
要注意的几点:
(1)、要把几个Command都要写好了,不然会报错。
(2)、DataTable的TableName一定要定义好,这个自然和你更新的表名相同了。
(3)、传进来的DataSet不要使用了AcceptChanges()这个方法,以此类推DataTable和DataRow。不然更新不了数据库的记录。
(4)、如果数据库是有自增字段的话,且要取出自增值,建议不要使用UpdateDataSet()这个方法,虽然是可以实现。
- 关于微软ADO.NET提供的组件库里的UpdateDataSet()的用法心得
- ADO.NET的主要组件
- 微软提供的数据访问组件SqlHelper
- ADO.NET 内置的数据提供程序
- ADO.NET提供的Connection类总结
- ADO.NET的基本用法
- 最近学习ADO.NET 的一些心得
- 微软的 DAO,RDO,OLE DB, ADO,ADO.NET
- ado.net里的datatable与xml
- 关于ADO.NET中的几大对象的介绍使用的个人心得
- 在delphi.net的VCL.net里使用Ado.net
- 在delphi.net的VCL.net里使用Ado.net
- Net 中使用 ADO.NET 的一些心得
- Log4net开源组件的用法心得
- ADO.NET连接数据库的方式(官方提供)
- ADO.net 中数据库连接方式(微软提供)
- ADO.net 中数据库连接方式(微软提供)
- ADO.net 中数据库连接方式(微软提供)
- MOSS MSN开发群-皮皮1月31日主题:SSRS 与MOSS集成讲座
- Windows同步API (一)—— Mutex
- OASIS宣布在2008年将举行一次关于SOA中的可组合性的研讨会
- 通用日期转化
- 一个GUI例子 Linux ----gshortcut
- 关于微软ADO.NET提供的组件库里的UpdateDataSet()的用法心得
- 中国惠普前总裁孙振耀谈人生
- QT4.3.3+VC6.0安装编译过程详解
- Exception is thrown but exception.getMessage() is null
- C++ Builder 快捷键
- MFC程序员的WTL指南之包容ActiveX
- ADO.Net中通过HashTable对SqlParameter赋值的三种方法和返回DataTable的两种方式
- asp.net 中上传并读取Excel文件(OLEDB方式)
- 7种js分页效果