读取器关闭时无效的读取尝试(VB.Net)

时间:2017-12-10 19:58:29

标签: mysql database vb.net sqldatareader datareader

我目前在我的功能中遇到此错误。这种情况是我需要向每个计划发送到当前时间的客户发送一条短信,所以我有一个SQL查询来选择具有持续计划的条目以及SQL数据读取器(READER)读取的每个条目,它将执行我的短信代码。但是我收到了这个错误。

这是我的代码:

Public Function onschedule()
    MysqlConn.ConnectionString = ServerString

    Dim READER As MySqlDataReader
    Dim mobileNum, customername, datestart, msg As String

    Try
        MysqlConn.Open()
        Dim query As String

        query = "SELECT DATE_FORMAT(ratedemand_datestart, '%M %d, %Y') as 'Date Start',ratedemand_contact as 'Contact Number',sales_customername as 'Customer Name' FROM bnb.ratedemand WHERE ratedemand_status = 'Reserved' AND NOW() = ratedemand_datestart AND rate_category='Billiard'"
        COMMAND = New MySqlCommand(query, MysqlConn)
        READER = COMMAND.ExecuteReader

        While READER.Read
            mobileNum = READER.GetString("Contact Number") '09856994598'
            datestart = READER.GetString("Date Start")     'November 2, 2017 12:00 AM'
            customername = READER.GetString("Customer Name") 'Bruce R. Wayne'

            'SMS Code Starts Here'

            If SerialPort.IsOpen Then
                SerialPort.Close()
            End If

            msg = customername + ", this is an automated text from Sta. Lucia East Grand Mall Billiard Hall confirming your 'Billiard Schedule' on " + datestart + " (Right Now). We are reminding you that if you are 20 minutes behind your schedule. It will be automatically cancelled."

            SerialPort.PortName = "COM4"
            SerialPort.BaudRate = 9600
            SerialPort.Parity = Parity.None
            SerialPort.StopBits = StopBits.One
            SerialPort.DataBits = 8
            SerialPort.Handshake = Handshake.RequestToSend
            SerialPort.DtrEnable = True
            SerialPort.RtsEnable = True
            SerialPort.NewLine = vbCrLf
            SerialPort.Open()

            If SerialPort.IsOpen() Then
                SerialPort.Write("AT" & vbCrLf)
                SerialPort.Write("AT+CMGF=1" & vbCrLf)
                SerialPort.Write("AT+CMGS=" & Chr(34) & mobileNum & Chr(34) & vbCrLf)
                SerialPort.Write("Message: " & msg & Chr(26))
            Else
                MetroMessageBox.Show(Admin_Menu, "Port not Available", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End While

        MysqlConn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        MysqlConn.Dispose()
    End Try

    Return True
End Function

1 个答案:

答案 0 :(得分:0)

不要低估关闭读者的必要性:)。

Try
  While Reader.Read()
    ...
  End While
Finally
  Reader.Close
  MysqlConn.Close()
End Try