我正在将一个经典的asp网站转换为.NET,并且可以对这个问题使用一些建议。
原始代码:
Dim oCountCMD
oCountCMD = Server.CreateObject("ADODB.Command")
With oCountCMD
.ActiveConnection = Application("ConnString")
.CommandText = "sp_GetSearchHistoryCount"
.CommandType = .CommandType.StoredProcedure
.oCountCMD(1) = strUser
.oCountCMD(2) = DateAdd("n", -1, Now())
.Execute()
If oCountCMD(0) > 60 Then
CheckSearchCounts = True
Else
CheckSearchCounts = False
End If
End With
oCountCMD = Nothing
我未完成的转换代码:
Dim objConnection As New SqlConnection(Application("ConnString"))
Dim oCountCMD As New SqlCommand("sp_GetSearchHistoryCount", objConnection)
With oCountCMD
.CommandType = CommandType.StoredProcedure
.Parameters.Add(New SqlParameter("@UserName", SqlDbType.VarChar, 20))
.Parameters("@UserName").Value = strUser
.Parameters.Add(New SqlParameter("@SearchDt", SqlDbType.DateTime))
.Parameters("@SearchDt").Value = DateAdd("n", -1, Now())
// need to execute here, and find number of rows/records returned
End With
如何捕获执行此存储过程时返回的行数?
答案 0 :(得分:1)
尝试
Dim theCount Integer
theCount = .ExecuteScalar()
答案 1 :(得分:1)
感谢您的评论更新。
如果存储过程的响应是单个值,那么您需要执行已设置的SqlCommand。如果您在此命令上ExecuteScaler()
,那么它将返回单个Sql类型,最有可能返回int,然后您可以将其转换为.net值。
希望它有所帮助: - )
答案 2 :(得分:1)
Dim countEffected As Integer=0
With oCountCMD
.CommandType = CommandType.StoredProcedure
.Parameters.Add(New SqlParameter("@UserName", SqlDbType.VarChar, 20, strUser))
.Parameters.Add(New SqlParameter("@SearchDt", SqlDbType.DateTime,,DateAdd("n", -1, Now())))
countEffected = .ExecuteScalar()
End With
'return true/false as previous VB6 did.
CheckSearchCounts = countEffected > 60