SQL执行后索引超出范围 [英] Index out of range after SQL execution
问题描述
我有一个用VB编写的内联查询SELECT语句。当我尝试运行它时,它会提示我一个错误消息IndexOutOfRangeException。我尝试调试它,并在ExecuteReader()之后找到错误提示。
起初我认为这是由于我的返回值为NULL但是在我的代码中添加了IsDBNull之后错误仍然存在。
以下是我的代码:
Hi, I have a inline query SELECT statement written in VB. When I try to run it, it prompt me an error message IndexOutOfRangeException. I tried to debug it and found the error prompt after ExecuteReader().
At first I thought it was due to my returned value is NULL but the error still persist after I have added IsDBNull in my codes.
Below are my codes:
Try
cnSql = New SqlConnection(SQL_CONNECTION_STRING)
cnSql.Open()
strSQL = "SELECT ISNULL(MIN(SH.shipping_start_date), GETDATE()) AS StartDate "
strSQL = strSQL + "FROM Shipping AS SH INNER JOIN Working AS WK ON SH.shipping_id = WK.shipping_id "
strSQL = strSQL + "WHERE WK.woNo = '" + number + "'"
cmSql = New SqlCommand(strSQL, cnSql)
readerSQL = cmSql.ExecuteReader()
If readerSQL.Read() Then
If Not IsDBNull(readerSQL.Item("shipping_start_date")) Then
woStartDate = readerSQL.Item("shipping_start_date")
End If
Else
ShipStartDate = DateTime.Now
End If
readerSQL.Close()
cnSql.Close()
cmSql.Dispose()
cnSql.Dispose()
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error!")'error happen here
我尝试过: < br $>
1.在我的代码中添加了IsDBNull来过滤掉从DB返回的NULL值。
2.试图直接在SQL Server中执行查询没有任何错误。
What I have tried:
1. Added IsDBNull in my codes to filter out NULL value returned from DB.
2. Tried to excute the query directly in SQL Server without any error.
推荐答案
我不是SQL专家,但看起来返回的列名为StartDate
并在代码中检查shipping_start_date
。
I am not SQL expert, but it looks like the returned column is namedStartDate
and you checkshipping_start_date
in your code.
这篇关于SQL执行后索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!