我想操作的是利用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. [转]php 在各种web服务器的运行模式

    一.php在apache中运行模式 php在apache中一共有三种工作方式:CGI模式.FastCGI模式.Apache 模块DLL) 以下分别比较: 1. CGI模式与模块模式比较: php在ap ...

  2. qq客服问题

    angularJs会给ng-href的不正常跳转,会 添加unsafe的前缀.原因是angular对href是有安全检查的,只能生成它认为安全的链接.解决方法如下: 在config.js中注入 fun ...

  3. find tar 压缩第一层目录,用于资料备份。

    find *.tar.gz -exec tar zxvf '{}' \;//查找当前目录下的.tar.gz 的文件的 并发送给后面的命令执行find . -maxdepth 1 //查找当前目录下的文 ...

  4. ORA-12170: TNS:Connect timeout occurred

    VM 作为ORACLE 服务器,客户端登陆提示超时,本地连接使用网络连接正常. D:>sqlplus system/oracle123@//192.168.63.121:15021/pdb01 ...

  5. mybatis 的一点问题

    写法1:   public User queryUserByUsername(String username); 写法2:   public User queryUserByUsername(@Par ...

  6. 导出Excel数据

    先要导入jxl架包,其中的abc.xls为测试Excel,具体代码如下,仅供参考: import java.io.File; import java.io.FileInputStream; impor ...

  7. 从市场运营角度谈Uber中国的第一批用户是怎么来的

    声明:这篇文章是从http://www.010lm.com/redian/2016/0312/1206875.html转来的,分享给大家. 1)首先告诉用户Uber是做什么的?即培养用户品牌意识. 我 ...

  8. Java中常见的5种WEB服务器介绍

    这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...

  9. php实现echo json_encode正确显示汉字

    <?php header('Content-type: text/html; charset=utf-8'); /* function showmessage($msg = '', $redir ...

  10. 8--UI 初步认识 简易计算器

    UI是App的根基:一个App应该是先有UI界面,然后在UI的基础上增加实用功能(2)UI相对简单易学:UI普遍是学习过程中最简单的一块,能快速拥有成就感和学习兴趣(3)UI至关重要:开发中的绝大部分 ...