【发布时间】:2014-08-14 07:33:44
【问题描述】:
我的 WinForm 中有这个子:
Public Function CheckSentToday(ByVal date1 As DateTime) As Boolean
Dim cmSql As New SqlCommand("Check_Today", cnSql)
Try
cmSql.CommandType = CommandType.StoredProcedure
cmSql.Parameters.AddWithValue("@date1", String.Format("{0:yyyy-MM-dd}", date1))
If Not cnSql Is Nothing AndAlso cnSql.State = ConnectionState.Closed Then cnSql.Open()
If cmSql.ExecuteScalar IsNot Nothing Then
Return True
Else
Return False
End If
Catch ex As Exception
WriteToText("CheckSentToday", ex.ToString)
CheckSentToday = False
Finally
If Not cnSql Is Nothing AndAlso cnSql.State = ConnectionState.Open Then cnSql.Close()
End Try
End Function
我在执行SqlCommand之前打开连接,
并在finally 子句中关闭连接
不过,每次调用这个 sub 时它都会返回以下错误:
Description:System.InvalidOperationException: Invalid attempt to call Read when reader is closed.
at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
at System.Data.SqlClient.SqlDataReader.Read()
at System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
谁能帮我找出原因?
任何帮助将不胜感激
【问题讨论】:
标签: vb.net ado.net sqlconnection sqlcommand