access数据文件的加密问题不能改变一个打开的共享数据库密码
用代码动态创建一个access数据库文件,并给access数据文件加密,出现以下错误提示:
代码:
Visual Basic code?Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim cmm As OleDbCommand Dim cnn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\a1.mdb") cnn.Open() cmm = New OleDbCommand("ALTER DATABASE PASSWORD 123456 null", cnn) cmm.ExecuteNonQuery() cnn.Close() cmm = Nothing cnn = Nothing End Sub
请大家帮个忙,有没有方法,直接在动态创建数据库文件时,就把密码也加进去呢?
Dim wrkDefault As Workspace
Dim dbsNew As Database
Dim tdfNew As TableDef
Dim NewDB As Database
Set wrkDefault = DBEngine.Workspaces(0)
Set dbsNew = wrkDefault.CreateDatabase("d:\NewDB.mdb", dbLangGeneral, dbEncrypt)
dbsNew.NewPassword "", "123" '设置数据库密码为123
dbsNew.Execute "Create Table Info(Term text(10),Tzh text(5))"
Set wrkDefault = Nothing
Set dbsNew = Nothing
MsgBox "数据库创建成功!"
Dim cat As Catalog = New Catalog() cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=C:\NewMDB.mdb;Jet OLEDB:Database Password=123456") 'Console.WriteLine("Database Created Successfully") ' 以下代码创建一个名为“测试项目表”单主键新表 Dim Cn As ADODB.Connection = New ADODB.Connection '定义连接 Dim objTable As ADOX.Table = New ADOX.Table '定义新表 Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\NewMDB.mdb;Jet OLEDB:Database Password=123456") '链接到数据库 objTable.Name = "测试项目表" '表命名 '给表“测试项目表” 创建一个新的字段,字段名“主键” objTable.Columns.Append("主键", , ADOX.DataTypeEnum.adInteger) '创建并追加一个新的主键。注意:我们只是通过“PimaryKey_Field”作为主键源列。新键被追加到“测试项目表”的键集 objTable.Keys.Append("主键", ADOX.KeyTypeEnum.adKeyPrimary, "主键") objTable.Columns.Append("数量", ADOX.DataTypeEnum.adInteger) '追加一个数字型字段 cat.Tables.Append(objTable) '把所有的新字段追加到表 objTable = Nothing Cn.Close() Cn = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(cat) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat) cat = Nothing GC.WaitForPendingFinalizers() GC.Collect()