我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库
代码:
SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=newsystem;Integrated Security=True");
  SqlDataAdapter da = new SqlDataAdapter("select * from comment", conn);
  //这里的构造函数直接实例化了SelectCommand所需要的SqlCommand了吧?
  DataSet ds = new DataSet("myds");
  da.FillSchema(ds, SchemaType.Source, "comment");
  da.Fill(ds, "comment");
  //接下来设置InsertCommand所需要的SqlCommand
SqlCommand incmd=new SqlCommand("insert into comment (****) values(****)",conn);
  da.InsertCommand = incmd;
  //接下来是更新到数据库
  da.Update(ds.Tables["comment"]);
  ds.Tables["comment"].AcceptChanges();
  可到数据库里一看,悲剧发生了:没有插入该条记录!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

网上一查:有些说要实例化SqlCommandBuilder,可我觉得我的InsertCommand所需要的SqlCommand都写好了,不需要这样吧!
SqlCommandBuilder好像只是适用于直接修改DataSet,由SqlCommandBuilder自动生成所需的SQL语句:
  SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=newsystem;Integrated Security=True");
  SqlDataAdapter da = new SqlDataAdapter("select * from comment", conn);
  SqlCommandBuilder cmdb = new SqlCommandBuilder(da);
  DataSet ds = new DataSet("myds");
  da.FillSchema(ds, SchemaType.Source, "comment");
  da.Fill(ds, "comment");
  ds.Tables["comment"].Rows[5]["content"] = "Can you help me???";
  da.Update(ds.Tables["comment"]);
  ds.Tables["comment"].AcceptChanges();

-----------------------------------------------------------------------------------------------------------------------------------------------

public static SqlDataAdapter CreateCustomerAdapter( SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(); // Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection); // Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; // Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection); // Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); adapter.InsertCommand = command; // Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command; // Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command; return adapter;
}
public static DataSet GetCustomerData(string dataSetName, string connectionString) { DataSet dataSet = new DataSet(dataSetName); using (SqlConnection connection = new SqlConnection(connectionString)) { SqlDataAdapter adapter = new SqlDataAdapter( "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", connection); DataTableMapping mapping = adapter.TableMappings.Add("Table", "Customers"); mapping.ColumnMappings.Add("CompanyName", "Name"); mapping.ColumnMappings.Add("ContactName", "Contact"); connection.Open(); adapter.FillSchema(dataSet, SchemaType.Mapped); adapter.Fill(dataSet); return dataSet; } }
摘自网络:http://q.cnblogs.com/q/20398/

我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库的更多相关文章

  1. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版 在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵 ...

  2. 【译】在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本

    原文 [译]在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本 本篇文章是讲述使用iTextSharp这个开源组件的系列文章的第三篇,iTextSharp可以通过As ...

  3. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版(转)

    [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版   在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵盖 ...

  4. 利用SqlDataAdapter进行分页

    利用SqlDataAdapter进行记录分页 说到分页,很多地方都会用到,不管是windows程序还是web程序,为什么要进行分页?很简单,如果BlueIdea BBS帖子列表不分页的话,几十万条记录 ...

  5. LINQ to SQL更新数据库操作(转载)

    使用LINQ to SQL建模Northwind数据库 在这之前一起学过LINQ to SQL设计器的使用,下面就使用如下的数据模型: 当使用LINQ to SQL设计器设计以上定义的五个类(Prod ...

  6. 利用Jquery使用HTML5的FormData属性实现对文件的上传

    1.利用Jquery使用HTML5的FormData属性实现对文件的上传 在HTML5以前我们如果需要实现文件上传服务器等功能的时候,有时候我们不得不依赖于FLASH去实现,而在HTML5到来之后,我 ...

  7. JavaScript---Dom树详解,节点查找方式(直接(id,class,tag),间接(父子,兄弟)),节点操作(增删改查,赋值节点,替换节点,),节点属性操作(增删改查),节点文本的操作(增删改查),事件

    JavaScript---Dom树详解,节点查找方式(直接(id,class,tag),间接(父子,兄弟)),节点操作(增删改查,赋值节点,替换节点,),节点属性操作(增删改查),节点文本的操作(增删 ...

  8. Pandas的基础操作(一)——矩阵表的创建及其属性

    Pandas的基础操作(一)——矩阵表的创建及其属性 (注:记得在文件开头导入import numpy as np以及import pandas as pd) import pandas as pd ...

  9. 利用BeanUtils在对象间复制属性

    commons-beanutils是jakarta commons子项目中的一个软件包,其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包含了大量的属性,很 ...

随机推荐

  1. Java良葛格 学习笔记《二》

    正则表达式 . 符合任一字符\d 符合0到9任一个数字字符\D 符合0-9以外的字符\s 符合'\t'.'\n'.'\x0B'.'\f'.'\r'等空格符\w 符合a到z.A到Z.0到9等字符,也就是 ...

  2. UIImageView的UserInteractionEnabled什么时候为no

    UIImageView作为背景,但直接把按钮或者UITextField放在上面无法相应事件 特殊子类的覆盖 userInteractionEnabled属性默认值为YES,但UIView的一些子类中对 ...

  3. C#连接Oracle的方法

    C#连接Oracle的方法 方法1: System.Data.OracleClient oracleConnectionString : data source = orcl;user id= sco ...

  4. Ansible3:ansible.cfg配置说明【转】

    Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码.是否开启sudo认证.actio ...

  5. 学习最短路建图 HUD 5521

    http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意:有n个点,m个集合,每个集合里面的点都两两可达且每条边权值都是val,有两个人A, B,A在po ...

  6. centos配置samba

    一.samba服务器的安装与配置 [root@localhost ~]# yum -y install samba samba-common samba-client        samba服务器所 ...

  7. Windows下的 Axel下载工具 - 移植自Linux

    Axel 是 CLI (command-line interface) 下的一个多线程下载工具,通常我都用它取代 wget 下载各类文件,适用于 Linux 及 BSD 等 UNIX 类平台. 以下是 ...

  8. Java学习笔记之多态

    1.父类型的引用可以指向子类型的对象: Parent p = new Child(); 2.当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误:如果有,再去调用子类的该同名方法 ...

  9. CSS3的一些前缀

    为了兼容多个浏览器,css3通常前面加一大堆前缀 -webkit  /*为Chrome/Safari*/-moz  /*为Firefox*/-ms   /*为IE*/-o  /*为Opera*/ -w ...

  10. JavaScript(1)——变量、函数声明及作用域

    这是我的第一篇博客文章,本人不才,文笔也不好,所以可能写的有点凌乱.有什么不对的地方还望见谅.不过每天进步一小步,总有一天会迈出那一大步.以下内容是我对变量.函数声明及函数表达式.作用域的理解. [变 ...