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. UsingSystem. IO; usingSystem. Data. OleDb; Connect to Access Data
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 Access Data
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:
1: string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName;
2:
3: // create a database
4: ADOX.Catalog catalog = new Catalog();
5: try
6: {
7: catalog.Create(conn);
8: }
9: catch
10: {}
11:
12:
13: // connect to the database
14: ADODB.Connection cn = new ADODB.Connection();
15: cn.Open(conn, null, null, -1);
16: catalog.ActiveConnection = cn;
17:
18: // create a table
19: ADOX.Table table = new ADOX.Table();
20: table.Name = "AdPlayList";
21:
22: ADOX.Column column = new ADOX.Column();
23: column.ParentCatalog = catalog;
24: column. Type = ADOX. DataTypeEnum. adInteger; // you must set the field Type first.
25: column.Name = "ID";
26: column.DefinedSize = 9;
27: column.Properties["AutoIncrement"].Value = true;
28: table.Columns.Append(column, DataTypeEnum.adInteger, 0);
29: // set the primary key
30: table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "ID", "", "");
31:
32: table.Columns.Append("FileName", DataTypeEnum.adVarWChar, 50);
33: table.Columns.Append("FileDate", DataTypeEnum.adDate, 0);
34: table.Columns.Append("FileSize", DataTypeEnum.adInteger, 9);
35: table.Columns.Append("OrderID", DataTypeEnum.adInteger, 9);
36: table.Columns.Append("Sha1", DataTypeEnum.adVarWChar, 50);
37:
38: try
39: {
40: catalog.Tables.Append(table);
41: }
42: catch (Exception ex)
43: {
44: MessageBox.Show(ex.Message);
45: }
46: // you must close the connection here. Otherwise, an error will occur when adding data.
47:
48: table = null;
49: catalog = null;
50: Application.DoEvents();
51: 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.
4: string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";
Jet OLEDB:Database Password=" + Pwd + ";Jet OLEDB:Engine Type=5";
Change Database Password:
The SQL statement for changing the Database Password is:
ALTERDATABASE 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.adModeShareExclusive;
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.
Code download
Http://cid-4b517bbe6c0b58a0.office.live.com/self.aspx/.Public/cnblogsCsharp/CreateAccess.rar