当读取器关闭时,我得到错误无效尝试调用元数据 [英] I get the error invalid attempt to call meta data when reader is closed

查看:71
本文介绍了当读取器关闭时,我得到错误无效尝试调用元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读器关闭时收到错误无效尝试调用元数据



我尝试过:



I get the error invalid attempt to call meta data when reader is closed

What I have tried:

Private Sub checkdetails()
    sqlconn = New SqlConnection(str)
    sqlconn.Open()
    Try
        cmd = New SqlCommand("Select BillNo from OrderBill Where Date='" & Format(dtptoday.Value, "MM/dd/yyyy") & "'", sqlconn)
        dc = cmd.ExecuteReader
        While dc.Read
            If ComboBox1.Text.Length > 0 And ComboBox1.Text = dc(0).ToString Then
                Check_Bill()
            End If
        End While
        dc.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    sqlconn.Close()

End Sub

推荐答案

永远不要为Sql命令文本使用连接字符串。使用参数例如(未选中)

Never use concatenated strings for your Sql command text. Use Parameters e.g. (unchecked)
cmd = New SqlCommand("Select BillNo from OrderBill Where Date=@date", sqlconn)
cmd.Parameters.AddWithValue("@date", dtptoday.Value)



使用调试确保 dc 实际设置为某种东西。你可能还想用


Use debugging to ensure that dc is actually set to something. You might also want to wrap the While loop with

If dc.HasRows Then
   ...
End If

另一个可能的原因是你已经在其他地方使用 dc 尝试将代码更改为

Another possible cause is that you have already used dc elsewhere try changing your code to

Dim dc = cmd.ExecuteReader


这篇关于当读取器关闭时,我得到错误无效尝试调用元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆