2011-09-13 5 views
0

をAdapter.Updateを()を使用して、私はこのようなアダプタを持っている - のCol1、Col2に、COL3ADO.NET - 挿入親表からの列のサブセットで

-

var ds = new DataSet("T1"); 
adapter.Fill(ds, "Table1"); 

は今、表1は、3つの列を持っています

私はCol1とCol3を持つ表2を持っています。上記のアダプタを使用して、のレコードを挿入するにはどうすればよいですか?を選択しました。表1から表2のカラムを選択しましたか?私はこれを試しましたが、運はありません。

// remove the column which is not required 
ds.Tables["Table1"].Columns.Remove("Col2"); 

// clear the old table mappings 
adapter.TableMappings.Clear(); 

// create new table mappings 
DataTableMapping mapping = adapter.TableMappings.Add("Table2", ds.Tables["Table1"].ToString()); 
mapping.ColumnMappings.Add("Col1", ds.Tables["Table1"].Columns[0].ColumnName); 
mapping.ColumnMappings.Add("Col3", ds.Tables["Table1"].Columns[2].ColumnName); 

// fill the adapter with new Dataset 
var newDs = ds.Copy(); 
adapter.Fill(newDs); 
ds.Dispose(); 

// Insert records into new Table 
recordsUpdated += adapter.Update(newDs , "Table2"); 

ERROR - 追加情報:と、SourceColumn 'Col2に' のDataTableの '表1' にDataColumnの 'Col2に' を欠落。

答えて

0

私はこれを行っている -

新しい表とa.Initialise SqlDataAdapterオブジェクトを(表2 SELECT * FROM) - あなたが作ることを確認してください

newAdapter 

それを呼び出す - AcceptChangesDuringFill = false、後で 行を列挙しているときに、rowstateを追加に設定すると、 が機能しません。

b.Removeこのデータテーブルの行状態表1

c.Updateのデータテーブルから、このような '追加された' への不要なカラム -

// 1st, commit all changes in the DataTable. 
dtTable1.AcceptChanges(); 

// update the row state 
foreach (DataRow row in dtTable1.Rows) 
{ 
    row.SetAdded(); 
} 

d.Finally '手順aで作成したアダプタを使用して「挿入」します。このように -

var recordsInserted = newAdapter.Update(dtTable1); // insert happens 
関連する問題