ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

adplus-dvertising
Using ADOX in the .NET Framework
Previous Home Next

ADOX, more formally the “Microsoft ADO Extensions for DDL and Security,” exposes an object model that allows data source objects to be created and manipulated. The ADOX object model is shown in the following figure. Not all data sources support all of the objects in the model; this is determined by the specific OleDb Data Provider

The top-level object, Catalog, equates to a specific data source. This will almost always be a database, but specific OleDb Data Providers might expose different objects. The Groups and Users collections control access security for those data sources that implement it The Tables object represents the tables within the database. Each table contains a Columns collection, which represents individual fields in the table; an Indexes collection, which represents physical indexes.

Create a Database Using ADOX
  1. In the code editor, select makedbin the Control Name combo box,and then select Click in the Method Name combo box. In the code editor, select makedbin the Control Name combo box,and then select Click in the Method Name combo box.
  2. In the form designer, double-click Make DB.Visual Studio adds the event handler to the code.
  3. Add the following lines to the event handler, specifying the path to the Sample DBs directory on your system where indicated:
  4. string dsStr, dsCN;
    
  5.  ADODB.Connection adcon = new ADODB.Connection();
    
  6. ADOX.Catalog mdb = new ADOX.Catalog();
    
  7. dsStr = "<<specify the path to the Sample DBs directory>>" _
    
  8. + "\\test.mdb";
    
  9. dsCN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
    dsStr + ";";
    
  10. adcon.ConnectionString = dsCN;
    
  11. mdb.Create(dsCN);
    
  12.  MessageBox.Show("Finished", "Make DB");
    
  13. Press F5 to run the application, and then click Make DB.
Add a Table to a Database Using ADOX
  1. In the code editor, select btnMakeTable in the Control Name combo box, and then select Click in the Method Name combo box. Visual Studio adds the event handler to the code.In the form designer, double-click Make Table.Visual Studio adds the event handler to the code.
  2. Add the following code to the event handler:
  3.  ADODB.Connection cnADO;
    
  4. ADOX.Catalog mdb = new ADOX.Catalog();
    
  5.  ADOX.Table dt = new ADOX.Table();
    
  6. cnADO = create_connection();
    
  7. cnADO.Open(cnADO.ConnectionString, "", "", -1);
    
  8.  mdb.ActiveConnection = cnADO;
    
  9. dt.Name = "New Table";
    
  10.  dt.Columns.Append("TableID",
    ADOX.DataTypeEnum.adWChar, 5);
    
  11. dt.Columns.Append("Value",
    ADOX.DataTypeEnum.adWChar, 20);
    
  12. dt.Keys.Append("PK_NewTable",
    ADOX.KeyTypeEnum.adKeyPrimary, "TableID");
    
  13. mdb. Tables.Append(dt);
    
  14.  MessageBox.Show("Finished", "Make Table");
    
  15. Press F5 to run the application, and then click Make Table.
Previous Home Next