在.NET应用程序的Access的打开实例中执行功能 [英] Execute a function in an open instance of Access from a .NET application

查看:74
本文介绍了在.NET应用程序的Access的打开实例中执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Access数据库和一个C#应用程序. C#应用程序对Access中的某些表进行了某些操作,我想在C#代码结束时刷新Access表单.我尝试这样做:

I have an Access database and a C# app. The C# app does some things to some tables in Access and I want to refresh the Access form when the C# code ends. I tried to do this:

void refresh()
    {
Access.Application acApp = new Access.ApplicationClass();//create msaccess application

object oMissing = System.Reflection.Missing.Value;
//Run the Test macro in the module
acApp.Run("Function_refresh",ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing
,ref oMissing,ref oMissing);
acApp.Quit();//exit application
        }

如果数据库关闭,我的代码将起作用.如果数据库已经打开,如何使用该功能?

My code works if the DB is closed. How can I use the function if the database is already open?

C#在Access表中做了一些插入,所以当它结束时,我想在打开窗体时刷新它.

edit: The C# does some inserts in an Access table, so when it ends I want to refresh the form when it is open.

推荐答案

如果关闭了数据库,我的代码将起作用.如果数据库已经打开,如何使用该功能?

My code works, if the DB is closed. How can I use the function if the database is already open?

我刚刚在C#Winforms应用程序上为按钮尝试了以下代码,它对我有用.如果我在Access中打开了数据库,并且在数据库中打开了"LogForm"表单,那么当我单击C#表单上的按钮时,Access表单就会更新.

I just tried the following code for a button on a C# Winforms application and it worked for me. If I have my database open in Access and the form "LogForm" is open in the database then when I click the button on my C# form the Access form is updated.

private void button1_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Access.Application acApp;
    this.Activate();
    acApp = (Microsoft.Office.Interop.Access.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Access.Application");
    Microsoft.Office.Interop.Access.Dao.Database cdb = acApp.CurrentDb();
    cdb.Execute("INSERT INTO LogTable (LogEntry) VALUES ('Entry added ' & Now())");
    acApp.Forms["LogForm"].Requery();
}

该代码改编自Microsoft支持文章

The code is adapted from the Microsoft Support article

如何使用Visual C#自动执行Office程序的运行实例

这篇关于在.NET应用程序的Access的打开实例中执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆