C # dynamically create an Access database and password

Source: Internet
Author: User

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.

 
 
  1. Using System. IO;
  2. Using System. Data. OleDb; // connect to the Access Database
  3. Using ADOX;
  4. // Reference COM: Microsoft ADO Ext. 2.8 for DDL and Security
  5. // Add reference: Microsoft ActioveX Data Objects 2.8 Library

Create a database:

Then, use ADODB to create a database and check the Code directly:

 
 
  1. String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + fileName;
  2. // Create a database
  3. ADOX. Catalog catalog = new Catalog ();
  4. Try
  5. {
  6. Catalog. Create (conn );
  7. }
  8. Catch
  9. {}
  10. // Connect to the database
  11. ADODB. Connection cn = new ADODB. Connection ();
  12. Cn. Open (conn, null, null,-1 );
  13. Catalog. ActiveConnection = cn;
  14. // Create a table
  15. ADOX. Table table = new ADOX. Table ();
  16. Table. Name = "AdPlayList ";
  17. ADOX. Column column = new ADOX. Column ();
  18. Column. ParentCatalog = catalog;
  19. Column. Type = ADOX. DataTypeEnum. adInteger; // you must set the field Type first.
  20. Column. Name = "ID ";
  21. Column. DefinedSize = 9;
  22. Column. Properties ["AutoIncrement"]. Value = true;
  23. Table. Columns. Append (column, DataTypeEnum. adInteger, 0 );
  24. // Set the primary key
  25. Table. Keys. Append ("PrimaryKey", ADOX. KeyTypeEnum. adKeyPrimary, "ID ","","");
  26. Table. Columns. Append ("FileName", DataTypeEnum. adVarWChar, 50 );
  27. Table. Columns. Append ("FileDate", DataTypeEnum. adDate, 0 );
  28. Table. Columns. Append ("FileSize", DataTypeEnum. adInteger, 9 );
  29. Table. Columns. Append ("OrderID", DataTypeEnum. adInteger, 9 );
  30. Table. Columns. Append ("Sha1", DataTypeEnum. adVarWChar, 50 );
  31. Try
  32. {
  33. Catalog. Tables. Append (table );
  34. }
  35. Catch (Exception ex)
  36. {
  37. MessageBox. Show (ex. Message );
  38. }
  39. // You must close the connection. Otherwise, an error occurs when adding data.
  40. Table = null;
  41. Catalog = null;
  42. Application. DoEvents ();
  43. 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.

 
 
  1. // Create a connection statement without a password
  2. String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + fileName;
  3. // Create a password-based connection statement. pwd is the password.

Change Database Password:

The SQL statement for changing the Database Password is:

 
 
  1. 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:

 
 
  1. String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + fileName + "; Jet OLEDB: Database password =" + openpwd;
  2. String SQL = "ALTER DATABASE PASSWORD" + newpwd + "" + openpwd;
  3. ADODB. Connection cn = new ADODB. Connection ();
  4. Cn. Mode = ADODB. ConnectModeEnum. admodemo-exclusive;
  5. Cn. Open (conn, null, null,-1 );
  6. // Execute the SQL statement to change the password.
  7. Object num;
  8. Cn. Execute (SQL, out num,-1 );
  9. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.