zoukankan
html css js c++ java
DataAdapter数据集DataSet和数据库的同步(3):使用CommandBuilder来更新数据集
/**/
/*
--===------------------------------------------===---
CommandBuilder:
如果DataTable映射到单个数据库表或者从单个数据库表生成,
可以利用CommandBuilder对象自动生成DataAdapter的3个命令:
DeleteCommand, UpdateCommand, InsertCommand.
为了生成Insert,Update,Delete语句,CommandBuilder会自动
使用SelectCommand属性来检索所需的元数据集.
SelectComandText里面必须要有主键字段,否则无法Builder~!
许明会 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);
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
]);
}
}
;
}
查看全文
相关阅读:
北京高考零分作文(看到最后一句笑喷了!)
关于前几天无法访问的问题
用 PHP 读取和编写 XML DOM[转]
Delphi对INI文件的详细操作方法
如何在WebService中获取客户端的IP地址
正则表达式30分钟入门教程
[原创]shell对xml操作的脚本
预防SQL注入攻击之我见(好文章)
表驱动方法(非常好的数据结构)
请教shell读写XML问题
原文地址:https://www.cnblogs.com/flaaash/p/1011128.html
最新文章
LeetCode: Minimum Window Substring
LeetCode: Merge Sorted Array
LeetCode: Minimum Depth of Binary Tree
LeetCode: Multiply Strings
LeetCode: Merge Two Sorted Lists
[转]深入理解C语言指针的奥秘
彻底搞定C指针
HTML5设计原理
在linux下面使用mtrace来检查一般程序的内存溢出
彻底搞定C指针-指向另一指针的指针
热门文章
堆和栈的区别(转过无数次的文章)(转)
NSLog/NSString 输出格式 Format String Issue
Linux shell脚本的字符串截取
XCode快捷键
定义表格的指定列的属性
博客搬迁说明兼旧博留恋
北京高考零分作文(看到最后一句笑喷了!)
好片推荐《WALL·E》(又名《机器人瓦力》
Delphi对INI文件的详细操作方法
如何在WebService中获取客户端的IP地址
Copyright © 2011-2022 走看看