Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using the code below, I add columns:
public void AlterTable_AddColumn(OracleConnection connection, string tblName, string colName)  {
    DataTable table = GetOracleDataTable(connection, "select * from ", tblName);
    DataColumnCollection columns = table.Columns;

    if (!columns.Contains(colName))    {
        string s = "Alter table " + tblName.ToUpper() + " Add ( " + colName + " VARCHAR2(500) ) ";
        using (OracleCommand cmd = new OracleCommand(s, connection))  {
            cmd.ExecuteNonQuery();
        }
    }
    connection.Close();
}

After adding 1 new column (termed Column1 here), the code iterated for adding another column (Column2). In this step, I viewed variable: columns, which listed all of the column names except for Column1 that was just added. In order to refresh the table with all of the updated columns/rows information, how can the table be refreshed? In this piece of code, I erloaded the datatable after inserting a new column (see DataTable table = ...). I also tried connection.Close() and then connection.Open(), but not effective. Will be glad to listen to your advisory for the solution. Thanks.
Posted
Updated 30-Jun-14 8:50am
v2

1 solution

I revised the code as below:
C#
public void AlterTable_AddColumn(OracleConnection connection, DataTable tbl, string colName)   {
    //DataTable table = GetOracleDataTable(connection, "select * from ", tblName);
    DataColumnCollection columns = tbl.Columns;  // table.Columns;

    if (!columns.Contains(colName))    {
        string s = "Alter table " + tbl.TableName.ToUpper() + " Add ( " + colName + " VARCHAR2(500) ) ";
        using (OracleCommand cmd = new OracleCommand(s, connection))    {
            cmd.ExecuteNonQuery();
            //System.Windows.Forms.MessageBox.Show("Table altered");
        }
        tbl.Columns.Add(colName, typeof(string));
        tbl.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed);
        tbl.AcceptChanges();
    }
    connection.Close();
}
private static void Column_Changed(object sender, DataColumnChangeEventArgs e)   {
}    // useless

Then, the bug is solved. However, I actually did on both Oracle's insert and datatable update. I know it somehow redundant. If you have a betgter idea, please share. Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900