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
Copyright © 2011-2022 走看看