在VB.NET中可以用两种方式调用Access数据库:ADO.NET或者自动化(Automation)。
如果你是要操纵Access下的数据那么ADO.NET必然是首选。如果你要操纵Access下的Form,Report就必须使用自动化。
以下是一个示例:
- 新建一个Window Forms Application.
- 引用”Microsoft.Office.Interop.Access” 14.0(因为我的实现环境是MS Access 2010,Visual Studio 2010)
- 数据库情况如下:
- 代码如下:
Imports Access = Microsoft.Office.Interop.Access Imports MyLog4Net Public Class Form1 Private mlog As New MyLog4Net.MyLogger(GetType(Form1),"Debug.log") Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click Dim OAccess As Access.Application Dim OFD As New OpenFileDialog Try OFD.Multiselect = False OFD.Filter = "Access File 2010|*.accdb" OFD.ShowDialog() OAccess = New Access.Application OAccess.OpenCurrentDatabase(OFD.FileName,False) OAccess.Visible = True OAccess.DoCmd.OpenReport("TestTable1Report",Access.AcView.acViewPreview) Catch ex As Exception mlog.ERROR(ex.Message) End Try End Sub End Class