Reader 关闭时读取的尝试无效 (VB.Net)
Invalid attempt to Read when Reader is Closed (VB.Net)
我目前在我的函数中遇到这个错误。情况是我需要向每个当前时间安排的客户发送一条短信,因此我有一个 SQL 查询 Select 具有正在进行的时间表的条目,并且对于每个条目SQL 数据Reader (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
不要低估关闭 reader :)
的必要性
Try
While Reader.Read()
...
End While
Finally
Reader.Close
MysqlConn.Close()
End Try
我目前在我的函数中遇到这个错误。情况是我需要向每个当前时间安排的客户发送一条短信,因此我有一个 SQL 查询 Select 具有正在进行的时间表的条目,并且对于每个条目SQL 数据Reader (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
不要低估关闭 reader :)
的必要性Try
While Reader.Read()
...
End While
Finally
Reader.Close
MysqlConn.Close()
End Try