我想操作的是利用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. JPG、PNG和GIF图片的基本原理及优化方法

    一提到图片,我们就不得不从位图开始说起,位图图像(bitmap),也称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的.这些点可以进行不同的排列和染色以构成一副图片.当放大位图时,可以看见 ...

  2. htop安装步骤【原创】

    htop安装步骤 下载:http://hisham.hm/htop/releases/ [root@hchtest2 ~]# tar zxvf htop-2.0.2.tar.gz [root@hcht ...

  3. oracle中的turnc,round,floor,ceil,coalesce函数

    这四个函数有点类似java中的函数,首先是 trunc(number,[decimals]) 这个函数类似截取函数 number:表示你要输入的数 decimals(小数): 表示你要截取的位数[正数 ...

  4. shell 入门教程

    打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好.     一  惯例,第一个shell #!/bin/bash echo "Hello ...

  5. 在idea的maven项目使用el或jstl表达式

    必须加上这句: <%@ page isELIgnored="false" %> 否则无法解析el或jstl表达式 <%@ taglib prefix=" ...

  6. Socks

    Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  7. CSS position 属性

    position: relative | absolute | static | fixed 参考网站:http://blog.csdn.net/dyllove98/article/details/8 ...

  8. Zookeeper: configuring on centos7

    thispassage is referenced, appreciated. ZooKeeper installation: Download from this site Install java ...

  9. c++数组指针bug

    ClassA* csList = ]; ClassA ca = csList[]; ca.x=; CCLOG(].x);//output: caList[0].x -431602080.000000 ...

  10. cocos2d-x-2.2.6创建工程

    1.下载并安装Python,我的版本是2.7.3,不要太高,据说不能用.2.配置环境变量,将Python的安装的根目录加入系统环境变量的Path路径中(不赘述,自行配置)3.配置好之后打开cmd命令行 ...