请查看以下架构:
CREATE TABLE Person (id int not null identity,[index] varchar(30),datecreated datetime)
insert into Person ([index],datecreated) values ('4,5,6','2011-01-01')
insert into Person ([index],datecreated) values ('1,2,3','2011-02-02')
insert into Person ([index],datecreated) values ('7,8','2012-02-02')
和下面的代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim _ConString As String = WebConfigurationManager.ConnectionStrings("dbConnection").ConnectionString
Dim connection As New SqlConnection(_ConString)
Dim objCommand As New SqlCommand
Dim objDR As SqlDataReader
Dim sqlString As String
sqlString = "SELECT * FROM Person WHERE datecreated < '2012-01-01' "
objCommand.CommandText = sqlString & " ORDER BY left (substring([index],charindex(',',[index])+1,200), " & _
" charindex(',',substring([index],charindex(',',[index])+1,200))-1)"
objCommand.Connection = connection
connection.Open()
objDR = objCommand.ExecuteReader
If objDR.HasRows Then
MsgBox("Has Rows")
Else
MsgBox("No Rows")
End If
connection.Close()
Catch ex As Exception
End Try
End Sub
此代码是实时系统中功能的一部分。每当我使用 ORDER BY 以开发模式(或实时)运行完整的应用程序时;DataReader 没有记录,并且出现一个消息框,显示 No Rows(当我单独运行上述代码时不会发生这种情况)。注释掉 ORDER BY 子句后返回正确的行数。没有抛出异常。有没有办法查看 SQLDataReader 是否产生了错误?
更新请不要发布有关内存泄漏的答案,例如连接未关闭等或未处理异常的事实。我意识到这一点。我生成了上面的代码来尝试重新创建问题。
UPDATE2 23/05/2012 19:30 gmt 我做了一些进一步的测试,当使用参数化查询时会出现差异,即执行命令对象后,SQL Studio 管理器中会返回一行,但应用程序中不会返回一行。我知道参数化查询被缓存。参数化执行计划可能与无参数化执行计划不同的原因是什么?