使用命令构建器SqlCommandBuilder

// 使用命令构建器SqlCommandBuilder
// SqlCmdBuilder.cs
using   System;
using   System.Data;
using   System.Data.SqlClient;
namespace   Ch13
{
      class   SqlCmdBuilder
    {
          static   void   Main(   string [] args)
        {
              string   strConn =   "server=.\\MSSQL2012;integrated security=true;database=Northwind" ;
              string   qry =   "select * from employees where employeeid>8"   ;           
            SqlConnection conn =   new   SqlConnection(strConn);
              try
            {
                SqlDataAdapter da =   new   SqlDataAdapter();
                da.SelectCommand =   new   SqlCommand(qry, conn);
                  // 为了使命令构建器工作,数据适配器的SelectCommand属性
                  // 必须包含一个查询,它返回数据库表的主键或唯一键。
                  // 如果该键不存在,就会生成InvalidOperation异常,不会生成命令。               
                  // 不必写带参数的SQL,以及为SqlCommand添加参数映射的代码。
                  // 创建命令构建器。
                SqlCommandBuilder cb =   new   SqlCommandBuilder(da);

                DataSet ds =   new   DataSet();
                da.Fill(ds,   "emp" );
                DataTable dt = ds.Tables[   "emp" ];

                  Console .WriteLine(   "添加前" );
                  foreach   (DataRow row   in   dt.Rows)
                {
                      Console .WriteLine(   "{0} {1} {2} {3}"
                        , row[   "employeeid" ].ToString().PadRight(5)
                        , row[   "firstname" ].ToString().PadRight(15)
                        , row[   "lastname" ].ToString().PadRight(15)
                        , row[   "city" ]);
                }
                  // 添加新行
                DataRow newrow = dt.NewRow();
                newrow[   "firstname" ] =   "张"   ;
                newrow[   "lastname" ] =   "三"   ;
                newrow[   "titleofcourtesy" ] =   "先生"   ;
                newrow[   "city" ] =   "深圳"   ;
                newrow[   "country" ] =   "中国"   ;
                dt.Rows.Add(newrow);

                  // 由于新行是添加到内存表,因此employeeid是未知的,不会显示
                  Console .WriteLine(   "添加后" );
                  foreach   (DataRow row   in   dt.Rows)
                {
                      Console .WriteLine(   "{0} {1} {2} {3}"
                        , row[   "employeeid" ].ToString().PadRight(5)
                        , row[   "firstname" ].ToString().PadRight(15)
                        , row[   "lastname" ].ToString().PadRight(15)
                        , row[   "city" ]);
                }
                  // 提交到数据库               
                da.Update(ds,   "emp" );
            }
              catch (   Exception   ex)
            {
                  Console .WriteLine(   "出错了:"   + ex);
            }
              finally
            {
                conn.Close();
                  Console .ReadLine();
            }
        }
    }
}
---------------------
添加前
9     Anne            Dodsworth       London
添加后
9     Anne            Dodsworth       London
      张               三               深圳


来自为知笔记(Wiz)


转载于:https://www.cnblogs.com/bloodfist/p/3278860.html

你可能感兴趣的:(使用命令构建器SqlCommandBuilder)