Using ADOX with ADO.NET


Microsoft ActiveX Data Objects Extensions for Data Definition Language and Security (ADOX) is an extension to ADO that provides an object model to manipulate data definition and security.

You can use ADOX in managed code in a similar way that you used an ADO recordset in the previous section. Just add a reference to ADOX type library using VS .NET's Add Reference option and use the namespace and its members as you would in unmanaged code.

To test this, create a new Windows application. After that, add a reference to the Microsoft ADO Ext. 2.7 for DLL and Security component to the project (see Figure 20-4).

click to expand
Figure 20-4: Adding a reference to ADOX library

After adding the reference, you'll see that the ADOX namespace is listed in the available namespaces in your application (see Figure 20-5).


Figure 20-5: ADOX namespace listed in project references

Now, the only thing you need to do is to use the namespace and its members. The ADOX classes should be available in your application after adding Imports ADOX to the project. ADOX provides objects that you can use to create databases, tables, and columns. The Catalog object represents a database and its Create method creates a new database. The Table object represents a database table. You can add columns to a Table object using the Columns collection's Append method. Listing 20-2 creates a new Microsoft Access database called Test.mdb with the table MyTable and two columns, col1 and col2, in it.

Listing 20-2: Using ADOX from Managed Code

start example
 Private Sub Form1_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load     ' Create SQL and Connection strings     Dim ConnectionString As String = _     "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\Test.mdb"     Try       ' Create a Catalog object       Dim ct As Catalog = New Catalog()       ct.Create(ConnectionString)       ' Create a table and two columns       Dim dt As Table = New Table()       dt.Name = "MyTable"       dt.Columns.Append("col1", DataTypeEnum.adInteger, 4)       dt.Columns.Append("col2", DataTypeEnum.adVarWChar, 255)       ' Add table to the tables collection       ct.Tables.Append(CType(dt, Object))       Catch exp As Exception         MessageBox.Show(exp.Message.ToString())       End Try   End Sub 
end example




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net