Thursday, 27 June 2013

Using ADOX in the .NET Framework

Using ADOX in the .NET Framework

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

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.


1. In the form designer, double-click Make DB.

Visual Studio adds the event handler to the code.


2. Add the following lines to the event handler, specifying the path to the


Sample DBs directory on your system where indicated:

3. string dsStr, dsCN;

4. ADODB.Connection adcon = new ADODB.Connection();

5. ADOX.Catalog mdb = new ADOX.Catalog();

6. dsStr = "<<specify the path to the Sample DBs directory>>" _

7. + "\\test.mdb";

8. dsCN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +

dsStr + ";";

9. adcon.ConnectionString = dsCN;

.10. mdb.Create(dsCN);

11. MessageBox.Show("Finished", "Make DB");

12. 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.

No comments: