In my previous work, I needed a brand new Access database. I could copy the database, or put the new database into the resource. I felt uncomfortable when I released the new database, or dynamic generation.
To generate a database, you must use ADO to add a reference first.
- Using System. IO;
- Using System. Data. OleDb; // connect to the Access Database
- Using ADOX;
- // Reference COM: Microsoft ADO Ext. 2.8 for DDL and Security
- // Add reference: Microsoft ActioveX Data Objects 2.8 Library
Create a database:
Then, use ADODB to create a database and check the Code directly:
- String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + fileName;
- // Create a database
- ADOX. Catalog catalog = new Catalog ();
- Try
- {
- Catalog. Create (conn );
- }
- Catch
- {}
- // Connect to the database
- ADODB. Connection cn = new ADODB. Connection ();
- Cn. Open (conn, null, null,-1 );
- Catalog. ActiveConnection = cn;
- // Create a table
- ADOX. Table table = new ADOX. Table ();
- Table. Name = "AdPlayList ";
- ADOX. Column column = new ADOX. Column ();
- Column. ParentCatalog = catalog;
- Column. Type = ADOX. DataTypeEnum. adInteger; // you must set the field Type first.
- Column. Name = "ID ";
- Column. DefinedSize = 9;
- Column. Properties ["AutoIncrement"]. Value = true;
- Table. Columns. Append (column, DataTypeEnum. adInteger, 0 );
- // Set the primary key
- Table. Keys. Append ("PrimaryKey", ADOX. KeyTypeEnum. adKeyPrimary, "ID ","","");
- Table. Columns. Append ("FileName", DataTypeEnum. adVarWChar, 50 );
- Table. Columns. Append ("FileDate", DataTypeEnum. adDate, 0 );
- Table. Columns. Append ("FileSize", DataTypeEnum. adInteger, 9 );
- Table. Columns. Append ("OrderID", DataTypeEnum. adInteger, 9 );
- Table. Columns. Append ("Sha1", DataTypeEnum. adVarWChar, 50 );
- Try
- {
- Catalog. Tables. Append (table );
- }
- Catch (Exception ex)
- {
- MessageBox. Show (ex. Message );
- }
- // You must close the connection. Otherwise, an error occurs when adding data.
- Table = null;
- Catalog = null;
- Application. DoEvents ();
- Cn. Close ();
Create a password database:
It is difficult for me to create an encrypted database, because the password is opened exclusively in Access and then added. Therefore, I always want to encrypt the database after it is created. After I try it, I fail. Finally, I want to change my mind. How can I add a password when I generate it.
- // Create a connection statement without a password
- String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + fileName;
- // Create a password-based connection statement. pwd is the password.
Change Database Password:
The SQL statement for changing the Database Password is:
- ALTER DATABASE PASSWORD [NewPassword] [OldPassword]
Using OleDbConnection to open the database and execute this SQL statement is useless, so I use ADODB to open the database for execution, but the following error is reported:
When you add a password to Access, you must enable the password in an exclusive mode. Therefore, you must set the mode to enable the Access. The details are as follows:
ADO ConnectModeEnum indicates whether to set or return the value of one of the following ConnectModeEnum.
Constant description
Default Value of AdModeUnknown. Indicates that the permission has not been set or cannot be determined.
AdModeRead indicates that the permission is read-only.
AdModeWrite indicates that the permission is write-only.
AdModeReadWrite indicates that the permission is read/write.
AdModeShareDenyRead prevents other users from using the read permission to open the connection.
AdModeShareDenyWrite prevents other users from using the write permission to open the connection.
Admodemo-exclusive prevents other users from opening connections.
AdModeShareDenyNone prevents other users from using any permission to open the connection.
The code for changing the database is as follows:
- String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + fileName + "; Jet OLEDB: Database password =" + openpwd;
- String SQL = "ALTER DATABASE PASSWORD" + newpwd + "" + openpwd;
- ADODB. Connection cn = new ADODB. Connection ();
- Cn. Mode = ADODB. ConnectModeEnum. admodemo-exclusive;
- Cn. Open (conn, null, null,-1 );
- // Execute the SQL statement to change the password.
- Object num;
- Cn. Execute (SQL, out num,-1 );
- Cn. Close ();
If you forget to add a password, you can use the password display tool "crack password unaccess", in the code folder.
Original article title: C # dynamically create an Access database, create a password-Based Access database, and change the Access password
Link: http://www.cnblogs.com/wk986/archive/2010/09/11/1823948.html