ADO.net与MS Access查询包含模块功能 [英] ADO.net with MS Access query containing a module function

查看:78
本文介绍了ADO.net与MS Access查询包含模块功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为 GetContactCategory 的MS Access查询



 选择 GetCategoryNames(contactId)  CategoryNames 来自联系人





函数 GetCategoryNames 是在同一数据库的模块中编写的函数。



现在,当我使用ADO.net在我的c#应用程序中调用此查询时,它会抛出错误。



以下是我的c#代码



 使用(OleDbCommand cm =  new  OleDbCommand())
{
cm.Connection = AccessConnection();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = GetContactCategory;

cm.Connection.Open();
使用(OleDbDataReader reader = cm.ExecuteReader()) // 此行引发错误
{
// 一些代码
}
}





获取未定义函数' GetCategoryNames <的错误/ b>'在表达中。



问题是什么,解决方案是什么。

解决方案

你无法在Access外部的查询中使用VBA函数。这些查询只在Access中执行时才有效。


ADO.NET不支持这种功能。使用 DAO [ ^ ](不是推荐,因为它很老了。

您需要将整个查询代码传递给 OleDbCmmand.CommandText



这是一个非常有趣的教程:为初学者使用ADO.NET [ ^

I have created an MS Access Query named GetContactCategory

select GetCategoryNames(contactId) as CategoryNames from Contacts



The Function GetCategoryNames is the function written in a module of same database.

Now when i call this query in my c# application using ADO.net it throws error.

Following is my c# code

using (OleDbCommand cm = new OleDbCommand())
            {
                cm.Connection = AccessConnection();
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "GetContactCategory";

                cm.Connection.Open();
                using (OleDbDataReader reader = cm.ExecuteReader())//This line throws error
                {
                    //some code
                }
           }



The error which is get Undefined function 'GetCategoryNames' in expression.

what is the problem and whats the solution.

解决方案

You can't use VBA functions in a query from outside of Access. Those kind of queries only work when executed inside Access.


ADO.NET does not supports this kind of functionality. It's possible to use queris togheter with DAO[^] (not recommended, because it's very old).
You need to pass entire code of query to OleDbCmmand.CommandText.

Here is quite interesting tutorial: Using ADO.NET for beginners[^]


这篇关于ADO.net与MS Access查询包含模块功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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