zoukankan      html  css  js  c++  java
  • 我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库

    我想操作的是利用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/
  • 相关阅读:
    20145216 史婧瑶《信息安全系统设计基础》第一周学习总结
    20145216史婧瑶《信息安全系统设计基础》第0周学习总结
    20145216《java程序设计》课程总结
    20145216史婧瑶《Java程序设计》第10周学习总结
    20145216史婧瑶《Java程序设计》第五次实验报告
    20145216史婧瑶《Java程序设计》第四次实验报告
    20145216史婧瑶《Java程序设计》第9周学习总结
    20145216史婧瑶《Java程序设计》第三次实验报告
    20145216史婧瑶《Java程序设计》第8周学习总结
    20145220韩旭飞第五周博客
  • 原文地址:https://www.cnblogs.com/qiushuixizhao/p/3236601.html
Copyright © 2011-2022 走看看