SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", nwindConn);
SqlCommandBuilder custCB = new SqlCommandBuilder(custDA);DataSet custDS = new DataSet();nwindConn.Open();
custDA.Fill(custDS, "Customers");// Code to modify data in the DataSet here.// Without the SqlCommandBuilder, this line would fail.
custDA.Update(custDS, "Customers");
nwindConn.Close();

解决方案 »

  1.   

    // Create a new Connection and SqlDataAdapter    SqlConnection myConnection = new SqlConnection("server=(local)\\VSdotNET;Trusted_Connection=yes;database=northwind");
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Customers", myConnection);
        DataSet myDataSet = new DataSet();
        DataRow myDataRow;
        
        // Create command builder. This line automatically generates the update commands for you, so you don't 
        // have to provide or create your own.
        SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
        
        // Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
        // key & unique key information to be retrieved unless AddWithKey is specified.
        mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        
        mySqlDataAdapter.Fill(myDataSet, "Customers");
        
        myDataRow = myDataSet.Tables["Customers"].NewRow();
        myDataRow["CustomerId"] = "NewID";
        myDataRow["ContactName"] = "New Name";
        myDataRow["CompanyName"] = "New Company Name";
        
        myDataSet.Tables["Customers"].Rows.Add(myDataRow); //添加    myDataSet.Tables["Customers"].Rows[0]["ContactName"]="Peach";修改   myDataSet.Tables["Customers"].Rows[0].Delete(); //删除mySqlDataAdapter.Update(myDataSet, "Customers");