在VS.NET C#中运行Microsoft Access模块 [英] Run Microsoft Access Module in VS.NET C#

查看:78
本文介绍了在VS.NET C#中运行Microsoft Access模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Visual Studio 2003程序中使用Microsoft Access Automation
$ C $ b用C#编写的Windows窗体。当我在VS.NET

程序中单击一个按钮时,我希望它运行Microsoft Access模块​​。


这是Microsoft Access上的信息模块:

Microsoft Access 2003 mdb位于:C:\ C #.NET\Esperanza

调用mdb:EspThr.mdb

模块被调用:Module1

函数是:公共函数WriteHTML()作为整数

函数的结果是它将文件写入C:\ UploadData


我找到了知识库文章317114,所以我认为我需要使用自动化。

但我从来没有使用它并需要帮助。我需要在

VS.NET中安装其他任何东西吗?我是否需要添加另一个Using语句?我应该在

中将我的

放入我的

private void btnAccess_Click(object sender,System.Eventargs e){}事件。为了运行该模块?


我已经使用System.Data.OleDB在ADO.NET中成功使用此Microsoft Access EspThr.mdb

。但我不认为我可以通过这种方式运行Microsoft Access

模块。请告诉我运行模块需要做什么。


提前致谢,

Pam

I need to use Microsoft Access Automation within a Visual Studio 2003 program
written in C# for Windows Forms. When a button is clicked in my VS.NET
program, I want it to run a Microsoft Access Module.

Here is the info on the Microsoft Access Module:
Microsoft Access 2003 mdb is located at: C:\C#.NET\Esperanza
The mdb is called: EspThr.mdb
The Module is called: Module1
The function is: Public Function WriteHTML() As Integer
The result of the function is that it writes files to C:\UploadData

I found Knowledge Base Article 317114, so I think I need to use Automation.
But I''ve never used it and need help. Do I need to install anything else in
VS.NET? Do I need to add another Using statement? What statement(s) should I
put in my
private void btnAccess_Click(object sender, System.Eventargs e) { } event in
order to run the module?

I already am successfully using this Microsoft Access EspThr.mdb in ADO.NET
using System.Data.OleDB. But I don''t think I can run the Microsoft Access
Module that way. Please tell me what I need to do to run the module.

Thanks in advance,
Pam

推荐答案

嗨Pam,


这是示例代码。


private void button1_Click(对象发送者,系统.EventArgs e)

{

Access.Application acApp = new Access.ApplicationClass(); //创建msaccess

应用程序

acApp.OpenCurrentDatabase(@" C:\ temp \db1.mdb",false,null); //打开mdb文件

object oMissing = System.Reflection.Missing。价值;

//在模块中运行Test宏

acApp.Run(" Test",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(); //退出应用程序

}


您可以尝试根据您的情况调整代码。


祝你好运,


Peter Huang

微软在线合作伙伴支持


安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有授予任何权利。

Hi Pam,

Here is the sample code.

private void button1_Click(object sender, System.EventArgs e)
{
Access.Application acApp = new Access.ApplicationClass();//create msaccess
application
acApp.OpenCurrentDatabase(@"C:\temp\db1.mdb",false ,null);//open mdb file
object oMissing = System.Reflection.Missing.Value;
//Run the Test macro in the module
acApp.Run("Test",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
}

You may try to tweak your code according your scenario.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


彼得,

抱歉,我无法让它工作,可能是因为我不够好。


1.我想我错过了一个参考资料。它不明白

Access.Application。我的使用应该是什么?声明是?或者请非常具体,并告诉我还需要做些什么才能让它理解。我没有

程序COM对象,所以我对此一无所知。


2.我尽力将你投入的东西转换成什么东西我可以用。

我唯一想做的就是运行一个特定的Microsoft Access模块​​

称为WriteHTML()所以这就是我写的内容,但我不知道

是对的。我只是不太了解。

(以下是Microsoft Access模块​​的信息:

Microsoft Access 2003 mdb位于:C:\C#。 NET\Esperanza

调用mdb:EspThr.mdb

调用模块:Module1

函数是:Public Function WriteHTML( )作为整数)


private void btnHTML_Click(object sender,System.EventArgs e)

{

Access.Application acAPP =新的AccessibleEvents.ApplicationClass();

acApp.OpenCurrentDatabase(@" C:\ C#.NET \EsperazaThre adsProgram\EspThr.mdb",

false, null);

object oMissing = System.Reflection.Missing.Value;

object rt = acApp.Run(" WriteHTML()");

acApp.Quit(Access.AcQuitOption.acQuitSaveNone);

}


我不明白你使用System.Reflection或者oMissing,所以我怀疑它会起作用。我做对了吗?我很抱歉理解

这么少。我真的不想编写com对象,我只想知道

足以使旧的东西运行直到微软取消旧的

技术。对不起,如果我冒犯了你或者我是在浪费你的时间。


谢谢,

Pam
Peter,
Sorry, I couldn''t get it to work, probably because I''m just not good enough.

1. I think I''m missing a reference. It doesn''t understand
Access.Application. What should my "using" statement be? Or please be very
specific and tell me what else I need to do to make it understand. I do not
program COM objects so I don''t know anything about this.

2. I tried my best to convert what you put into something I could use. The
only thing I really want to do is run a specific Microsoft Access module
called "WriteHTML()" so here is what I wrote, but I don''t have any idea if
it is right. I just don''t understand enough.
(Here is the info on the Microsoft Access Module:
Microsoft Access 2003 mdb is located at: C:\C#.NET\Esperanza
The mdb is called: EspThr.mdb
The Module is called: Module1
The function is: Public Function WriteHTML() As Integer)

private void btnHTML_Click(object sender, System.EventArgs e)
{
Access.Application acAPP = new AccessibleEvents.ApplicationClass();
acApp.OpenCurrentDatabase(@"C:\C#.NET\EsperazaThre adsProgram\EspThr.mdb",
false, null);
object oMissing = System.Reflection.Missing.Value;
object rt = acApp.Run("WriteHTML()");
acApp.Quit(Access.AcQuitOption.acQuitSaveNone);
}

I didn''t understand your use of System.Reflection or the oMissing, so I
doubt that it will work. Did I do it right? I apologize for understanding
so little. I really don''t want to program com objects, I just want to know
enough to make older stuff run until Microsoft does away with the older
technology. Sorry if I offend you or if I am wasting your time.

Thanks,
Pam


>我想我错过了一个参考资料。它不明白
> 1. I think I''m missing a reference. It doesn''t understand
Access.Application。


转到添加参考,右键单击解决方案中的项目

explorer。在COM选项卡中,双击Microsoft Access#。#Object

Library。你不必只需点击它一次,你必须确保它

进入底部的方框。

我应该使用什么?声明是?


使用Access;


但是如果你明确指定它就像你在下面那样你不需要它。 />
2.我尽力将你所用的东西转换成我可以使用的东西。
我唯一想做的就是运行一个特定的Microsoft Access模块​​
称为WriteHTML()所以这就是我写的,但我不知道
如果它是对的。


它看起来不错......它有用吗?

我只是不太了解。


基本上它是创建一个Access.Application的实例,然后打开数据库的
,然后告诉Access在该数据库中运行该函数,

然后关闭它而不保存。

(以下是Microsoft Access模块​​上的信息:
Microsoft Access 2003 mdb位于:C:\ C# .NET \ Esperanza
调用mdb:EspThr.mdb
模块被调用:Module1
函数是:Public Function WriteHTML()As Integer)

> private void btnHTML_Click(object sender,System.EventArgs e)
{/> Access.Application acAPP = new AccessibleEvents.ApplicationClass();
acApp.OpenCurrentDatabase(@" C:\ C# .NET \EsperazaThre adsProgram\EspThr.mdb",
false,null);
object oMissing = System.Reflection.Missing.Value;
object rt = acApp.Run(" WriteHTML()");
acApp.Quit(Access.AcQuitOption.acQuitSaveNone);
}

我不明白你的使用System.Reflection或oMissing,所以我怀疑它会起作用。


我不知道为什么你有这个
Access.Application acAPP = new AccessibleEvents.ApplicationClass();
$ b在我看来,$ b应该是

Access.ApplicationClass acAPP = new Access.ApplicationClass();

但要测试它看看哪些有效,哪些无效,并且如果你得到一个

的例外,你不知道它意味着什么,告诉什么例外是什么,b $ b和什么行扔了它。


涉及System.Reflection.Missing.Value的行看起来没有被使用,

所以你也可以删除它。但这是常量,你应该把

传递给任何你不想指定的可选参数的方法

a的价值。

仅作为示例:

如果我有一个Excel.Workbooks对象,并且我想调用

的Open方法 - 我需要传递大量的Missing.Value,因为该方法发生了很多可选参数,并且因为c#不支持可选的

参数,所以你不能把它们留空。

我可以写

xlWbk = xlWbks.Open(ExcelFileName,false,true,

Missing.Value,Missing。价值,Missing.Value,Missing。价值,Missing.Value,

Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,

Missing.Value,Missing.Value);

但对我来说看起来很糟糕。所以我写了

xlWbk =(Excel.Workbook)

((xlWbks.GetType())。InvokeMember(" Open",BindingFla gs.InvokeMethod,null,xlWbks ,

new object [] {ExcelFileName,false,true}));
使用COM对象的Type对象属性来调用
使用后期绑定的
a方法,它将一个对象数组传递给

方法。编译器没有解析并链接到我在编译时调用

的方法,所以我不介意我没有通过参数我

不在乎。 Excel也没有,因为它的参数是

可选。另外,请确保您不要在Acces 10.0

上使用这些技术,然后尝试在Access 9.0上使用它。

您需要进行所有开发在你需要使用的最低版本,

或使用后期绑定。在Access 9上开发并在Access上使用

10应该没问题。

我做得对吗?我很抱歉理解
这么少。我真的不想编写com对象,我只是想知道
足以让旧的东西运行直到微软取消旧的
技术。对不起,如果我冒犯了你或者我是在浪费你的时间。

谢谢,
Pam
Access.Application.
Go to Add Reference, from right clicking on the project in solution
explorer. From the COM tab, double click "Microsoft Access #.# Object
Library". You don''t just have to click on it once, you have to make sure it
goes into the box at the bottom.
What should my "using" statement be?
using Access;

but you don''t need it if you''ve explicitly specified it like you have below.
2. I tried my best to convert what you put into something I could use.
The
only thing I really want to do is run a specific Microsoft Access module
called "WriteHTML()" so here is what I wrote, but I don''t have any idea
if
it is right.
It looks OK... does it work?
I just don''t understand enough.
Basically it''s creating an instance of an Access.Application, then opening
your database, then telling Access to run the function in that database,
then closing it without save.
(Here is the info on the Microsoft Access Module:
Microsoft Access 2003 mdb is located at: C:\C#.NET\Esperanza
The mdb is called: EspThr.mdb
The Module is called: Module1
The function is: Public Function WriteHTML() As Integer)

private void btnHTML_Click(object sender, System.EventArgs e)
{
Access.Application acAPP = new AccessibleEvents.ApplicationClass();
acApp.OpenCurrentDatabase(@"C:\C#.NET\EsperazaThre adsProgram\EspThr.mdb",
false, null);
object oMissing = System.Reflection.Missing.Value;
object rt = acApp.Run("WriteHTML()");
acApp.Quit(Access.AcQuitOption.acQuitSaveNone);
}

I didn''t understand your use of System.Reflection or the oMissing, so I
doubt that it will work.
I''m not sure why you''ve got
Access.Application acAPP = new AccessibleEvents.ApplicationClass();
in my mind it should be
Access.ApplicationClass acAPP = new Access.ApplicationClass();
but test it to see what works and what doesn''t, and post back if you get an
exception that you don''t know what it means, telling what the exception was
and what line threw it.

The line involving System.Reflection.Missing.Value doesn''t look to be used,
so you might aswell just remove it. But this is the constant you should pass
to any method that takes optional parameters that you don''t want to specify
a value for.

Just as an example:
If I have an Excel.Workbooks object, and I want to call the Open method of
it - I would need to pass lots of Missing.Value because that method happens
to have a lot of optional parameters, and since c# doesn''t support optional
parameters, you can''t just leave them blank.
I could write
xlWbk = xlWbks.Open(ExcelFileName, false, true,
Missing.Value,Missing.Value,Missing.Value,Missing. Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing. Value,Missing.Value,
Missing.Value,Missing.Value);
but to me that looks bad. So I write
xlWbk = (Excel.Workbook)
((xlWbks.GetType()).InvokeMember("Open",BindingFla gs.InvokeMethod,null,xlWbks,
new object[]{ExcelFileName, false, true}));
which is using the Type object property of the COM object in order to invoke
a method by using late binding, and it passes an array of objects to the
method. The compiler doesn''t resolve and link into what method I''m calling
at compile time, so it doesn''t mind that I haven''t passed the parameters I
don''t care about. And neither does Excel, because it''s parameters are
optional. Also make sure you don''t use these techniques on, say, Acces 10.0
and then try to use it on Access 9.0.
You need to either do all development on the lowest version you need to use,
or use late binding throughout. Developing on Access 9 and using on Access
10 should be OK though.
Did I do it right? I apologize for understanding
so little. I really don''t want to program com objects, I just want to
know
enough to make older stuff run until Microsoft does away with the older
technology. Sorry if I offend you or if I am wasting your time.

Thanks,
Pam



这篇关于在VS.NET C#中运行Microsoft Access模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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