Hi Arthuro
Wouldn't want to let my ui alter my database structure personally but then that's not the question, your alter statement appears wrong, the data type number is an olap type. In SQL you could choose int typically or numeric or something like that:
Data types (Transact-SQL) | Microsoft Docs[
^]
then you should consider if the field can be nulled which when adding dynamically to a table with data is more or less mandatory. If you don't want it to be, you'll update the table with valid values afterwards and alter it not not allow.
Now we get
From
OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 NUMBER", con);
cmd.ExecuteNonQuery();
To
OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 int null", con);
cmd.ExecuteNonQuery();
Not too certain on the OleDbCommand, i personally always use the System.Data.SqlClient namespace and the SqlCommand
oh you're using MS Access, that brings memories. In that case the change is to use NUMERIC and you'll get
To
OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 numeric", con);
cmd.ExecuteNonQuery();
And you won't have to consider if the field can be nulled
Then to add them dynamically i'd suggest using the datatable you're filling anyway as it's rather handy
var dt = new DataTable();
sda.Fill(dt);
var nameOfList = new List<string>(dt.Columns.Count);
foreach (DataColumn clmn in dt.Columns)
{
nameOfList.Add(clmn.Caption);
}
var toAddList = new List<string>();
foreach(DataRow row in dt.Rows)
{
if (!nameOfList.Contains(row["Column[key]"]))
{
toAddList.Add(row["Column[key]"].ToString());
}
}
foreach(string nameOfKey in toAddList)
{
}