• 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]);
            }

        }
    ;
    }
  • 相关阅读:
    自定义cell
    微信界面
    设置字体阴影
    Xcode 7新特性Lightweight Generics 轻量级泛型与__kindof修饰符
    @synchronized 是递归锁,类似NSRecursiveLock,递归调用不会引起死锁,而NSLock是非递归锁。
    Your build settings specify a provisioning profile with the UUID, no provisioning profile was
    NSOperationQueue与GCD
    Objective-C中继承和类别的比较:category&Inherit
    iOS 事件处理机制与图像渲染过程
    [iOS]用instancetype代替id作返回类型有什么好处?(转)
  • 原文地址:https://www.cnblogs.com/flaaash/p/1011128.html
Copyright © 2020-2023  润新知