我在一个名为“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 FunctionMs-访问过程"CallFunction"(sql视图):
SELECT Customers.ID, Striplead([ContactName]) AS a FROM Customers;C#代码:
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;
}
}请建议这种方法是否不正确。或者,如果有一种方法从C#调用ms-access函数来获取数据。我的目标是从ms访问获取修改过的数据,并在C#中填充数据。
发布于 2013-09-28 14:37:57
Access提供了从SQL查询调用用户定义的VBA函数的能力,但该功能仅限于Microsoft Access应用程序本身内部的查询。这是因为VBA函数需要执行Access应用程序的基础设施(特别是VBA "Expression“)。
在您的示例中,您可能能够使用C#代码使用Microsoft.Office.Interop.Access运行“真正的”访问查询,但这会给您提供一个DAO Recordset对象,而修改它来处理OleDbDataAdapter可能会带来更大的麻烦(如果可能的话)。
您最好还是重新创建C#中的VBA函数逻辑。
https://stackoverflow.com/questions/19043939
复制相似问题