ToProgramAccess control, not data, but the table structure of the Access database. The field is required.
Therefore, you must first reference it in the solution as follows:
Solution resource manager --> reference --> (right-click) Add reference --> com -->Microsoft ADO Ext. 2.8 for DDL and security
Note:
When an Access database is created, a connection is automatically created. To release the. LDB file, the connection must be closed. This connection is from the ADODB class, so many people have never been able to find a way to release the connection.
The method is as follows: Reference --> (right-click) Add reference --> com -->Microsoft ActiveX Data Objects 2.8 Library
With these two COM components, you can start a series of operations...
1. Create an Access database
String Dbname = " E: \ temp \\ " + Datetime. Now. millisecond. tostring () + " . MDB " ; // Note that the extension must be MDB; otherwise, the table cannot be inserted.
ADOX. catalogclass cat = New ADOX. catalogclass ();
Cat. Create ( " Provider = Microsoft. Jet. oledb.4.0; Data Source = " + Dbname + " ; " );
II.Create a table
ADOX. tableclass TBL = New ADOX. tableclass ();
TBL. parentcatalog = CAT;
TBL. Name = " Mytable " ;
III.Add an Automatically increasing field
ADOX. columnclass col = New ADOX. columnclass ();
Col. parentcatalog = CAT;
Col. Type = ADOX. datatypeenum. adinteger; // The field type must be set first.
Col. Name = " ID " ;
Col. properties [ " Jet oledb: Allow Zero Length " ]. Value = False ;
Col. properties [ " Autoincrement " ]. Value = True ;
TBL. Columns. append (COL, ADOX. datatypeenum. adinteger, 0 );
4.Add a text field
ADOX. columnclass col2 = New ADOX. columnclass ();
Col2.parentcatalog = CAT;
Col2.name = " Description " ;
Col2.properties [ " Jet oledb: Allow Zero Length " ]. Value = False ;
TBL. Columns. append (col2, ADOX. datatypeenum. advarchar, 25 );
The following sentence is very important:
Cat. Tables. append (TBL ); // Add tables to the database (very important)
5. Open and modify the ACCESS database
ADODB. Connection Conn = New ADODB. Connection (); // ADO connection object
Conn. Open ( " Provider = Microsoft. Jet. oledb.4.0; Data Source = InterOP. Portal. dll " , "" , "" , 0 );
ADOX. catalogclass Cat = New ADOX. catalogclass ();
Cat. activeconnection = Conn; // Set the active connection object
The following sentence is very important: (creation and modification are required)
// Convert to ADO connection and close
(Cat. activeconnection As ADODB. Connection). Close ();
Cat. activeconnection = Null ;
Cat = Null ;