我有一个访问数据库和一个存储的查询。这是我的查询...
SELECT MAX(CLNG(RIGHT(ProjectNum, 6))) AS LastDigits
FROM project_master_query
WHERE ((ProjectNum LIKE (IIF([@priorityDefID] = 4, "C*", "F*"))));
例如,当我运行此查询并传入 2 的 @priorityDefID 时,我会返回 LastDigits 的列名和包含正确值 1 的行。这是我的VB代码调用它...
'Project Class (Without adding unnecessary code)
Public Shared Function GenerateProjectNumber(ByVal priorityDefID As Integer) As String
Dim dt As DataTable = ProjectSQL.GetLastGeneratedNumber(priorityDefID)
Dim lastGeneratedNumber As Integer
If dt.Rows.Count > 0 Then
'Exception Occurs Below: DBNull cannot be cast to other types
lastGeneratedNumber = Convert.ToInt32(dt.Rows(0).Item(0)) ' Or .Item("LastDigits"))
End If
End Function
'ProjectSQL Class
Public Shared Function GetLastGeneratedNumber(ByVal priorityDefID As Integer) As DataTable
Dim parameterList As New List(Of DataParameter)
parameterList.Add(New DataParameter("@priorityDefID", priorityDefID, ParameterDirection.Input, OleDbType.Integer))
Return DAL.GetDataTableUsingReader("GetLastGeneratedNumber", parameterList)
End Function
现在如您所见,返回了一行,但它包含一个空值。当我在 Access 中运行查询并传入与通过 VB 传入的完全相同的值时,我得到了正确的值。当我通过代码运行它时,我得到一个空值。这里有什么突出我想念的吗?