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;
//
更新出错,继续处理
}
}
}
}
查看全文
相关阅读:
Spring Boot 中使用 @Transactional 注解配置事务管理
springboot 整合Swagger2的使用
Vue的参数请求与传递
SpringMVC的全局异常处理
SpringBoot集成MyBatis的Bean配置方式
Springboot整合通用mapper
个人作业——软件工程实践总结作业
团队作业第二次—项目选题报告(追光的人)
结对第二次—文献摘要热词统计及进阶需求
结对第一次—原型设计(文献摘要热词统计)
原文地址:https://www.cnblogs.com/flaaash/p/1011129.html
最新文章
java操作时间,将当前时间减一年,减一天,减一个月
记一次 TARS Docker 环境 安装
几款常用压测工具使用小结
win10下 使用docker搭建swoole环境
PV、TPS、QPS是怎么计算出来的?
矩阵中求最大二维矩阵
PHP操作文件
约瑟夫环问题解决采用闭包函数实现
分析nginx中access.log日志统计状态为500
快速排序算法分析解析
热门文章
跟着文档学python(三):zip (Python function, in 2. Built-in Functions)
数据库笔记
python 以 utf-8 格式写文件
豆瓣爬虫日志(一):豆瓣电影url类型
python 中的多态
跟着文档学python(二):time.time() 与 time.clock() 的对比与总结
SQL错误日志 2015_7_22
写代码时候要养成初始化对象的好习惯
MYSQL 更改root密码(命令行)
phpstorm8 + wamp2.5 调试项目 (xdebug)
Copyright © 2011-2022 走看看