visual basic 2012连接必须有效且开放

时间:2013-11-23 10:57:10

标签: mysql vb.net

我所拥有的是一个小型消息传递功能,它基本上可以向一个用户或一组用户发送消息。有两个mysql表;一个为使用该系统的用户调用用户,另一个用于存储消息的消息。系统可以发送给特定用户,但是当发送给一组人时,VB给我

  

处理了InvalidOperationException:连接应该是开放且有效的

代码如下。

Dim receiver, subject, message As String
Dim user As Integer
Dim MySqlConnection As New MySqlConnection
Dim MyAdapter As New MySqlDataAdapter
Dim command As New MySqlCommand
Dim mydata As MySqlDataReader
Private Sub sendButton_Click(sender As Object, e As EventArgs) Handles sendButton.Click

user = loginForm.user
If recieverTextBox.Text = "" And studentCheckBox.Checked = False And facultyCheckBox.Checked = True Then

        subject = subjectTextBox.Text
        message = messageRichTextBox.Text
        MySqlConnection = New MySqlConnection
        MySqlConnection.ConnectionString = "server=localhost; User ID=root; password=''; database=sis_db"
        Dim query = "insert into messages (date, sender, receiver, subject, message) select CURRENT_DATE, '" & user & "',user_id,'" & subject & "', '" & message & "' from users where user_type='faculty';"

        Try
            MySqlConnection.Open()
            command.CommandText = query
            MyAdapter.SelectCommand = command
            mydata = command.ExecuteReader 'the error message points here'
            MsgBox("Messages sent", MsgBoxStyle.OkOnly, Title:="SUCCESS!")
            recieverTextBox.Clear()
            subjectTextBox.Clear()
            messageRichTextBox.Clear()
        Catch ex As MySqlException
            MsgBox("DATABASE ERROR!")
        End Try

        MySqlConnection.Close()

    ElseIf

1 个答案:

答案 0 :(得分:0)

您可以将选择的current_date替换为DateTime.Now而不是

你测试了连接吗?它可能只是一个错字