使用 OLE 时如何绕过 MS Access 启动?

发布于 2024-07-10 23:43:56 字数 374 浏览 7 评论 0原文

我正在尝试使用 C# 和 OLE 从 Access 数据库自动导出 xml。 除了显示启动表单并且在没有用户干预的情况下不会关闭之外,一切都运行良好。

这是我到目前为止正在做的事情:

objAccess = CreateObject("Access.Application");
objAccess.OpenCurrentDatabase("C:\\MYDB.mdb", true); //true = open in exclusive mode
objAccess.ExportXML(0, "TestTable", "c:\\test.xml");
objAccess.CloseCurrentDatabase();
objAccess.Quit();

I am trying to do an automated xml export from an access database using C# and OLE. Everything is working perfectly except that the startup form is shown and won't close without user intervention.

Here's what I am doing so far:

objAccess = CreateObject("Access.Application");
objAccess.OpenCurrentDatabase("C:\\MYDB.mdb", true); //true = open in exclusive mode
objAccess.ExportXML(0, "TestTable", "c:\\test.xml");
objAccess.CloseCurrentDatabase();
objAccess.Quit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-07-17 23:43:56

无需打开 Access 即可将表输出为 XML,例如,在 VBScript 中:

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adPersistXML = 1

Dim strCon, cn, rs

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strCon = "Provider= Microsoft.Jet.OLEDB.4.0; Data Source=" & "C:\Docs\LTD.mdb"

cn.Open strCon
rs.Open "Select * from Table1", cn, adOpenStatic, adLockOptimistic

If Not rs.EOF Then
    rs.MoveFirst 
    rs.Save "C:\Docs\Table1.xml", adPersistXML
End If

rs.Close
cn.Close

It should not be necessary to open Access to output a table to XML, for example, in VBScript:

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adPersistXML = 1

Dim strCon, cn, rs

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strCon = "Provider= Microsoft.Jet.OLEDB.4.0; Data Source=" & "C:\Docs\LTD.mdb"

cn.Open strCon
rs.Open "Select * from Table1", cn, adOpenStatic, adLockOptimistic

If Not rs.EOF Then
    rs.MoveFirst 
    rs.Save "C:\Docs\Table1.xml", adPersistXML
End If

rs.Close
cn.Close
两相知 2024-07-17 23:43:56

据我所知,唯一的方法是使用 msaccess.exe 上的 /nostartup 开关

所以你必须使用 shell 命令来获取访问对象

我有一个打开数据库并返回对象的函数(这是你可以使用的 VBA 代码)必须转换为 C#)

Private Function OpenDatabaseWithShell(pDatabaseFullPath As String) As Access.Application

Dim AccObj As Access.Application

    On Error GoTo ErrorHandler

    Set OpenDatabaseWithShell = Nothing

    Dim cmd As String

    On Error Resume Next

    ' basically build full msaccess.exe path and append database name and command switches
    cmd = SysCmd(acSysCmdAccessDir) & "MSAccess.exe """ & psDatabaseFullPath & """"
    cmd = cmd & " /nostartup /excl"

    'start ms access with shell
    Shell PathName:=cmd

    Do 'Wait for shelled process to finish.
      Err = 0
      Set AccObj = GetObject(pDatabaseFullPath)
    Loop While Err <> 0

    On Error GoTo ErrorHandler

    'return access object
    Set OpenDatabaseWithShell = AccObj

NormalExit:
    Exit Function

ErrorHandler:
    'error logging here
    Exit Function

End Function

编辑:
这里有一个链接,指向一些执行类似操作的 VB.NET 代码。 向下滚动到“创建完整示例 Visual Basic .NET 项目”并查找“ShellGetDB”函数

As far as I know the only way is to use the /nostartup switch on msaccess.exe

So you have to use shell command to get the access object

I have a function that opens a database and returns the object (This is VBA code which you will have to convert to C#)

Private Function OpenDatabaseWithShell(pDatabaseFullPath As String) As Access.Application

Dim AccObj As Access.Application

    On Error GoTo ErrorHandler

    Set OpenDatabaseWithShell = Nothing

    Dim cmd As String

    On Error Resume Next

    ' basically build full msaccess.exe path and append database name and command switches
    cmd = SysCmd(acSysCmdAccessDir) & "MSAccess.exe """ & psDatabaseFullPath & """"
    cmd = cmd & " /nostartup /excl"

    'start ms access with shell
    Shell PathName:=cmd

    Do 'Wait for shelled process to finish.
      Err = 0
      Set AccObj = GetObject(pDatabaseFullPath)
    Loop While Err <> 0

    On Error GoTo ErrorHandler

    'return access object
    Set OpenDatabaseWithShell = AccObj

NormalExit:
    Exit Function

ErrorHandler:
    'error logging here
    Exit Function

End Function

EDIT:
Here's a link to some VB.NET code that does a similar thing. Scroll down to "Create the Complete Sample Visual Basic .NET Project" and look for the "ShellGetDB" function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文