zoukankan
html css js c++ java
DataAdapter数据集DataSet和数据库的同步(4):数据适配器事件
/**/
/*
--===------------------------------------------===---
CommandBuilder:
如果DataTable映射到单个数据库表或者从单个数据库表生成,
可以利用CommandBuilder对象自动生成DataAdapter的3个命令:
DeleteCommand, UpdateCommand, InsertCommand.
为了生成Insert,Update,Delete语句,CommandBuilder会自动
使用SelectCommand属性来检索所需的元数据集.
SelectComandText里面必须要有主键字段,否则无法Builder~!
★数据适配器事件★
1.OnRowUpdating:在数据行更新前执行
2.OnRowUpdated:在数据行更新后执行,可以检查单条更新于巨的执行结果.
其EventArgs属性表如下
Command 要执行的数据库命令
Errors 错误
Row 要更新的行
StatementType 要执行的命令类型,可能为增删改查之一
RecordsAffected 要影响的行数
TableMapping 更新所使用的DataTableMapping
许明会 2007年12月22日 23:24:25
--===------------------------------------------===---
*/
using
System;
using
System.Data;
using
System.Data.SqlClient;
namespace
xumh
{
public
class
runMyApp
{
static
void
ShowTable(DataTable dataTable)
{
foreach
(DataRow row
in
dataTable.Rows)
{
for
(
int
i
=
0
;i
<
dataTable.Columns.Count; i
++
)
Console.Write(
"
{0}\t
"
,row[i]);
Console.WriteLine();
}
}
static
void
Main()
{
SqlConnection cn
=
new
SqlConnection(
@"
server=.; database=northwind; integrated security=true
"
);
//
预订事件
SqlDataAdapter da
=
new
SqlDataAdapter(
"
select employeeid,firstname,lastname,title from employees
"
,cn);
da.RowUpdating
+=
new
SqlRowUpdatingEventHandler(Updating);
da.RowUpdated
+=
new
SqlRowUpdatedEventHandler(Updated);
//
显示原始数据
DataSet dsEmployees
=
new
DataSet();
da.Fill(dsEmployees);
ShowTable(dsEmployees.Tables[
0
]);
//
更改数据
Console.ReadLine();
dsEmployees.Tables[
0
].Rows[
0
][
"
FirstName
"
]
=
"
Nancy
"
;
//
XuMinghui
SqlCommandBuilder builder
=
new
SqlCommandBuilder(da);
//
SqlCommandBuilder
//
Console.WriteLine(da.UpdateCommand.CommandText);
//
查看自动生成的CommandText
da.Update(dsEmployees);
ShowTable(dsEmployees.Tables[
0
]);
}
//
Updating:控制数据集的共享冲突,数据行只能被修改一次
static
void
Updating(
object
adapter, SqlRowUpdatingEventArgs e)
{
Console.WriteLine(
"
RowUpdating:
"
+
e.StatementType.ToString());
switch
(e.StatementType)
{
case
StatementType.Update:
{
SqlConnection cn
=
new
SqlConnection(
@"
server=.; database=northwind; integrated security=true
"
);
string
strCmd
=
"
select firstname,lastname from employees where firstname='
"
+
e.Row[
"
firstname
"
,DataRowVersion.Original]
+
"
'
"
;
//
上面,取firstname得原始值,如果数据库中查不到,证明正在试图修改已经修改过的数据集
SqlCommand cmd
=
new
SqlCommand(strCmd,cn);
cn.Open();
if
(
0
==
cmd.ExecuteNonQuery())
{
Console.WriteLine(
"
数据已经修改过,数据集过时!
"
);
e.Status
=
UpdateStatus.ErrorsOccurred;
//
报错
}
cn.Close();
break
;
}
}
}
//
Updated:更新出错,继续处理
static
void
Updated(
object
adapter, SqlRowUpdatedEventArgs e)
{
if
(e.StatementType
==
StatementType.Update)
{
if
(e.Status
==
UpdateStatus.ErrorsOccurred)
e.Status
=
UpdateStatus.SkipCurrentRow;
//
更新出错,继续处理
}
}
}
}
查看全文
相关阅读:
maven打包时加入依赖包及加入本地依赖包
is_file和file_exists效率比较
window.open()详解及浏览器兼容性问题示例探讨
Zend Studio汉化失败,如何给Zend Studio进行汉化
HTML页面跳转的5种方法
PHP中的符号 ->、=> 和 :: 分别表示什么意思?
php中$this->是什么意思
关于define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -7));的理解
【精华】PHP网站验证码不显示的终结解决方案
php提示undefined index的几种解决方法
原文地址:https://www.cnblogs.com/flaaash/p/1011129.html
最新文章
MySQL float 与decimal 各中的区别。
HTTP与HTTPS的区别
【总结】两种 NIO 实现:Selector 与 Epoll
epoll浅析以及nio中的Selector
阻塞IO、非阻塞IO、同步IO、异步IO等
IO
站点部署 廖雪峰的官方网站
https://www.cnblogs.com/yuanchenqi/articles/6755717.html
为什么用IP无法访问网站,域名可以访问?
详述 hosts 文件的作用及修改 hosts 文件的方法
热门文章
如何理解Nginx, WSGI, Flask之间的关系
关于spring中<context:component-scan base-package="" />写法
Could not create and/or set value back on to object .
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter /struts2.1.3以前版本和之后版本区别/新版Eclipse找不到Java EE Module Dependencies选项
java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100/虚拟机调优
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
maven 动态版本 aliyun阿里云Maven仓库地址——加速你的maven构建
java集合框架01——总体框架一览
hibernate3和4中 HibernateSessionFactory中不同之处 The method applySettings(Map) from the type ServiceRegistryBuilder is deprecated - The type ServiceRegistryBuilder is deprecated
编译的时候找不到包 但是maven denpendencies已经有这个包 。或者myeclipse 为webroot eclipse为webContext需要修改
Copyright © 2011-2022 走看看