3
2つのテーブルを持つデータベースを作成した後、このコードを実行しますが、テーブルには何もしません。OleDbDataAdapter .Updateメソッドでスキーマの変更が維持されない
private void CreateConstraint(DataSet dataSet, string table1, string table2, string column1,
string column2)
{
string connectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\...\Database1.accdb";
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbDataAdapter daf1 = new OleDbDataAdapter("select * from " + table1,connection);
daf1.Fill(dataSet,table1);
OleDbDataAdapter daf2 = new OleDbDataAdapter("select * from " + table2,connection);
daf2.Fill(dataSet,table2);
ForeignKeyConstraint FornKey = new ForeignKeyConstraint("ForKeyCustOrder",
dataSet.Tables[table1].Columns[column1],dataSet.Tables[table2].Columns[column2]);
FornKey.DeleteRule = Rule.Cascade;
// Add the constraint, and set EnforceConstraints to true.
dataSet.Tables[table2].Constraints.Add(FornKey);
dataSet.EnforceConstraints = true;
dataSet.AcceptChanges();
daf1.Update(dataSet.Tables[table1]);
daf2.Update(dataSet.Tables[table2]);
}