Function 如何从C#调用ms access函数?

Function 如何从C#调用ms access函数?,function,module,ms-access-2007,Function,Module,Ms Access 2007,我在一个名为“StripLead”的模块中编写了一个ms访问函数。我做了一个叫做“CallFunction”的单行程序。现在,当我从ms access调用此过程时,它工作正常。但是当我从C#调用这个过程时,我得到了一个错误: 表达式中未定义函数“Striplead” Ms访问模块代码: Public Function StripLead(pstrPhrase As String) As String Dim strFirstWord As String Dim strReturn As

我在一个名为“StripLead”的模块中编写了一个ms访问函数。我做了一个叫做“CallFunction”的单行程序。现在,当我从ms access调用此过程时,它工作正常。但是当我从C#调用这个过程时,我得到了一个错误:

表达式中未定义函数“Striplead”

Ms访问模块代码:

Public Function StripLead(pstrPhrase As String) As String
  Dim strFirstWord As String
  Dim strReturn As String
  Dim intPos As Integer

  strReturn = pstrPhrase
  intPos = InStr(pstrPhrase, " ")
  If intPos > 0 Then
    strFirstWord = Left$(pstrPhrase, intPos - 1)
    Select Case strFirstWord
      Case "A", "An", "The"
      strReturn = Right$(pstrPhrase, Len(pstrPhrase) - intPos)
    End Select
  End If

StripLead = strReturn
End Function
Ms访问过程“调用函数”(sql视图):

C#代码:

请说明该方法是否错误。或者,如果有办法从C#调用ms access函数来获取数据。
我的目标是从ms access获取修改后的数据,并在C#中填充数据网格。

access提供了从SQL查询调用用户定义的VBA函数的功能,但该功能仅限于在Microsoft access应用程序本身内进行的查询。这是因为VBA函数需要Access应用程序的基础结构(特别是VBA“表达式服务”)来执行

在您的情况下,您可能可以使用
Microsoft.Office.Interop.Access
从C#code中运行一个“真正的”访问查询,但这将为您提供一个
DAO记录集
对象,如果可能的话,摆弄它来使用
OleDbDataAdapter
可能会带来更多麻烦

您最好在C#中重新创建VBA函数逻辑

SELECT Customers.ID, Striplead([ContactName]) AS a FROM Customers;
   public void CallStoredProcedure(string NewQry)
       {
           try
           {
            DataTable tbl = new DataTable();
            using (OleDbConnection connection = new OleDbConnection(ConnectionString))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = "CallFunction";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = connection;

                using (OleDbDataAdapter adp = new OleDbDataAdapter(cmd))
                {
                    adp.Fill(tbl);
                }
            }

         }
        catch(Exception ex)
        {
            throw;   
        }
    }