Is it possible to change the schema of an MS Access database using ADO.NET?

I need to modify the MS Acess database schema (.mdb) using code.

Since DDL Jet Jet statements (ALTER TABLE, etc.) are rather poorly documented, I would prefer to use some kind of object library such as DAO ( myDatabase.TableDefs("myTable").Fields.Append(myNewField)) or ADOX ( myCatalog.Tables("myTable").Columns.Append(myNewField)) or SMO (which is available only for SQL Server , similar syntax - you get the idea).

Is there something similar, like ADOX for ADO.NET, or am I sticking to using DDL statements or referencing old DAO / ADOX libraries?

+5
source share
1 answer

ddl. , googling, , db . ? , .

public bool doesFieldExist(string table, string field)
    {
        bool ret = false;
        try
        {
            if (!openRouteCon())
            {
                throw new Exception("Could not open Route DB");
            }
            DataTable tb = new DataTable();
            string sql = "select top 1 * from " + table;
            OleDbDataAdapter da = new OleDbDataAdapter(sql, routedbcon);
            da.Fill(tb);
            if (tb.Columns.IndexOf(field) > -1)
            {
                ret = true;
            }

            tb.Dispose();


        }
        catch (Exception ex)
        {
            log.Debug("Check for field:" + table + "." + field + ex.Message);
        }

        return ret;
    }


    public bool checkAndAddColumn(string t, string f, string typ, string def = null)
    {

        // Update RouteMeta if needed.
        if (!doesFieldExist(t, f))
        {
            string sql;
            if (def == null)
            {
                sql = String.Format("ALTER TABLE {0} ADD COLUMN {1} {2} ", t, f, typ);
            }
            else
            {
                sql = String.Format("ALTER TABLE {0} ADD COLUMN {1} {2} DEFAULT {3} ", t, f, typ, def);
            }
            try
            {
                if (openRouteCon())
                {
                    OleDbCommand cmd = new OleDbCommand(sql, routedbcon);
                    cmd.ExecuteNonQuery();
                    string msg = "Modified :" + t + " added col " + f;
                    log.Info(msg);
                    if (def != null)
                    {
                        try
                        {
                            cmd.CommandText = String.Format("update {0} set {1} = {2}", t, f, def);
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception e)
                        {
                            log.Error("Could not update column to new default" + t + "-" + f + "-" + e.Message);
                        }

                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                log.Error("Could not alter RouteDB:" + t + " adding col " + f + "-" + ex.Message);
            }

        }
        else
        {
            return true;

        }
        return false;
    }
+1

All Articles