我有一個訪問數據庫和一個存儲的查詢。這裏是我的查詢......爲什麼我的數據表單元素返回空值?
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傳遞的完全相同的值時,我獲得了正確的值。當我通過代碼運行它時,我得到一個空值。這裏有什麼突出的,我失蹤了嗎?
好吧,這奇蹟般地工作。我正在使用OleDB。現在,當我在訪問中運行存儲的查詢時,它將返回任何內容,但在我的應用程序中可用盡管我留下了括號。感謝您的幫助,我一直試圖全面解決這個問題。 –
很好。別客氣。看看你是否找到了我添加的有關'ALike'的註釋。就我個人而言,我更喜歡'喜歡',而不喜歡'喜歡',但有些人似乎不喜歡。 :-) – HansUp
不知道。這絕對是個好消息,它也可以在ALIKE的兩端工作。現在我可以前進:) –