O
Oli
Hello all,
I'm writing a program that reads an Xml file and updates/patches a database
with the information in the Xml file, e.g. add rows and columns to database
tables, and add constraints to tables.
I decided on using DataSet and OleDbDataAdapter, and there is no problem
adding new database rows to existing database tables. Adding new columns to
an existing database table is not working, could you please read the
following code snippet and tell me what I'm doing wrong.
m_oleDbConn = new OleDbConnection (strConn);
// Create the Data Adapter.
OleDbDataAdapter daPatch = new OleDbDataAdapter ("SELECT * FROM patch",
m_oleDbConn);
OleDbCommandBuilder cbPatch = new OleDbCommandBuilder (daPatch);
m_oleDbConn.Open ();
DataSet ds = new DataSet ();
string strDbTable = "patch";
daPatch.FillSchema (ds, SchemaType.Source, strDbTable);
daPatch.Fill (ds, strDbTable);
DataTable tPatch = ds.Tables[strDbTable];
if (tPatch != null) {
// Check if the patch database table has the "newcol" column.
DataColumn colGrp = tPatch.Columns["newcol"];
if (colGrp == null) {
// Add the "newgrp" column to the patch database table.
colGrp = new DataColumn ();
colGrp.ColumnName = "newcol";
colGrp.DataType = Type.GetType ("System.Decimal");
colGrp.AllowDBNull = true;
colGrp.Caption = "newcol";
tPatch.Columns.Add (colGrp);
System.Diagnostics.Trace.WriteLine (tPatch.Columns.Count.ToString ());
}
// Add rows
DataRow dr;
for (int i = 0; i < 5; i++) {
dr = tPatch.NewRow ();
dr["col1"] = "test" + i.ToString ();
dr["newcol"] = i + 1;
tPatch.Rows.Add (dr);
}
// Merge/Update the data adapter changes with the data source.
daPatch.Update (ds, strDbTable);
//ds.Merge (tPatch);
}
m_oleDbConn.Close ();
Best regards,
Oli
I'm writing a program that reads an Xml file and updates/patches a database
with the information in the Xml file, e.g. add rows and columns to database
tables, and add constraints to tables.
I decided on using DataSet and OleDbDataAdapter, and there is no problem
adding new database rows to existing database tables. Adding new columns to
an existing database table is not working, could you please read the
following code snippet and tell me what I'm doing wrong.
m_oleDbConn = new OleDbConnection (strConn);
// Create the Data Adapter.
OleDbDataAdapter daPatch = new OleDbDataAdapter ("SELECT * FROM patch",
m_oleDbConn);
OleDbCommandBuilder cbPatch = new OleDbCommandBuilder (daPatch);
m_oleDbConn.Open ();
DataSet ds = new DataSet ();
string strDbTable = "patch";
daPatch.FillSchema (ds, SchemaType.Source, strDbTable);
daPatch.Fill (ds, strDbTable);
DataTable tPatch = ds.Tables[strDbTable];
if (tPatch != null) {
// Check if the patch database table has the "newcol" column.
DataColumn colGrp = tPatch.Columns["newcol"];
if (colGrp == null) {
// Add the "newgrp" column to the patch database table.
colGrp = new DataColumn ();
colGrp.ColumnName = "newcol";
colGrp.DataType = Type.GetType ("System.Decimal");
colGrp.AllowDBNull = true;
colGrp.Caption = "newcol";
tPatch.Columns.Add (colGrp);
System.Diagnostics.Trace.WriteLine (tPatch.Columns.Count.ToString ());
}
// Add rows
DataRow dr;
for (int i = 0; i < 5; i++) {
dr = tPatch.NewRow ();
dr["col1"] = "test" + i.ToString ();
dr["newcol"] = i + 1;
tPatch.Rows.Add (dr);
}
// Merge/Update the data adapter changes with the data source.
daPatch.Update (ds, strDbTable);
//ds.Merge (tPatch);
}
m_oleDbConn.Close ();
Best regards,
Oli