使用VB.NET连接MySQL

时间:2011-02-26 22:53:37

标签: .net mysql vb.net

我正在尝试创建一个使用VB.NET连接到MySQL数据库的非常简单的程序。我的程序只有一个表单和一个标签。我设置我的MySQL数据库有一个表和一个字段(VARCHAR)

称为“Tab1”。我使用PHPMyAdmin手动将值“CLOSED”插入到Tab1字段中。我希望我的程序将字段的值更改为OPEN / CLOSED,我还希望我的表单上的标签文本

点击时也会改变。

到目前为止,我已将MySQL.data引用添加到我的项目中并添加了:

Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

到General_Declarations

我还声明了一个包含MySQL连接的全局变量:

Public Class Form1

    Dim conn As New MySqlConnection

这是我连接到MySQL的函数:

Private Function Connect(ByVal server As String, ByRef user As String, ByRef password As String, ByRef database As String)
        ' Connection string with MySQL Info
        conn.ConnectionString = "server=" + server + ";" _
        & "user id=" + user + ";" _
        & "password=" + password + ";" _
        & "database=" + database + ";"
        Try
            conn.Open()
            Return True
        Catch ex As MySqlException
            Return MsgBox(ex.Message)
        End Try
    End Function

我使程序连接到Form_Load上的MySQL,如下所示:

Connect("db4free.net", "boomun", "*******", "boomdb")

它连接正常,但这里是我遇到麻烦的地方....我希望当我点击Label1时,字段从CLOSED变为OPEN。确实如此,但在第二次点击时它不会改变。不知何故,我需要一个工作代码来更新表格中的Tab1字段。

这是我到目前为止所做的:

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As MySqlDataReader
        Dim SQL As String

        SQL = "SELECT * FROM `boomtable` WHERE `Tab1` = 'CLOSED'"

        myCommand.Connection = conn
        myCommand.CommandText = SQL
        myAdapter.SelectCommand = myCommand
        Try
            myData = myCommand.ExecuteReader()
            myData.Read()
            If myData.HasRows = 0 Then
                ' *** UPDATE boomtable SET Tab1 = CLOSED WHERE Tab1 = OPEN  ***
                Label1.Text = "CLOSED"
                myData.Close()
            Else
                Label1.Text = "OPEN"
                ' *** UPDATE boomtable SET Tab1 = OPEN WHERE Tab1 = CLOSED ***
                myData.Close()
            End If
        Catch ex As MySqlException
            MsgBox(ex.Message)
        End Try

    End Sub

UPDATE专线对我不起作用...有人可以提供工作代码吗?谢谢,我真的很感激!!

以下是项目的整个代码:

Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

Public Class Form1

    Dim conn As New MySqlConnection

    Private Function Connect(ByVal server As String, ByRef user As String, ByRef password As String, ByRef database As String)
        ' Connection string with MySQL Info
        conn.ConnectionString = "server=" + server + ";" _
        & "user id=" + user + ";" _
        & "password=" + password + ";" _
        & "database=" + database + ";"
        Try
            conn.Open()
            Return True
        Catch ex As MySqlException
            Return MsgBox(ex.Message)
        End Try
    End Function

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As MySqlDataReader
        Dim SQL As String

        SQL = "SELECT * FROM `boomtable` WHERE `Tab1` = 'CLOSED'"

        myCommand.Connection = conn
        myCommand.CommandText = SQL
        myAdapter.SelectCommand = myCommand
        Try
            myData = myCommand.ExecuteReader()
            myData.Read()
            If myData.HasRows = 0 Then
                ' *** UPDATE boomtable SET Tab1 = CLOSED WHERE Tab1 = OPEN  ***
                Label1.Text = "CLOSED"
                myData.Close()
            Else
                Label1.Text = "OPEN"
                ' *** UPDATE boomtable SET Tab1 = OPEN WHERE Tab1 = CLOSED ***
                myData.Close()
            End If
        Catch ex As MySqlException
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connect("db4free.net", "boomun", "boom123", "boomdb")
    End Sub

End Class

3 个答案:

答案 0 :(得分:0)

请尝试以下语句:

UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED'

当然使用ExecuteNonQuery方法来执行语句。如果你的意思是OPEN和CLOSE是变量,那么请使用参数(它更安全)。

答案 1 :(得分:0)

喜欢这个......

Else
    Label1.Text = "OPEN"
    SQL = "UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED'"
    myCommand.CommandText = SQL
    myCommand.ExecuteNonQuery()
    myData.Close()
End If

???

答案 2 :(得分:0)

这里的一个错误是使用全局连接对象。 .Net数据库连接使用称为连接池的功能,这意味着在大多数情况下,为每次调用数据库创建新的连接对象真的更好。您可以做的是使用全局来保持连接 string

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    Dim SQL As String = "SELECT * FROM `boomtable` WHERE `Tab1` = 'CLOSED'"

    Using rdrCn As New MySqlConnection(connectionString), _
          rdrCmd As New MySqlCommand(SQL, rdrCn)

        rdrCn.Open()

        Using myData As MySqlDataReader = rdrCmd.ExecuteReader()
            SQL = "UPDATE boomtable SET Tab1= @NewState WHERE Tab1 = @OldState"
            Using updCn As New MySqlConnection(connectionString), _
                  updCmd As New MySqlCommand(SQL, updcn)

                updCmd.Parameters.Add("@NewState", MySqlDbType.VarChar, 6)
                updCmd.Parameters.Add("@OldState", MySqlDbType.VarChar, 6)


                If myData.Read()
                    Label1.Text = "CLOSED"
                    updCmd.Parameters(0).Value = "CLOSED"
                    updCmd.Parameters(1).Value = "OPEN"
                Else
                    Label1.Text = "OPEN"
                    Label1.Text = "CLOSED"
                    updCmd.Parameters(0).Value = "OPEN"
                    updCmd.Parameters(1).Value = "CLOSED"
                End If
                updCn.Open()
                upCmd.ExecuteNonQuery()
            End Using
        End Using
    End Using
End Sub

但是这里更大的问题是为什么你要去数据库两次呢?

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
    Dim SQL As String = "UPDATE boomtable SET Tab1 = CASE WHEN Tab1='CLOSED' THEN 'OPEN' ELSE 'CLOSED' END;SELECT Tab1 FROM boomtable LIMIT 1;"

    Using cn As New MySqlConnection(connectionString), _
          cmd As New MySqlCommand(SQL)

       cn.Open()
       Label1.Text = CStr(cmd.ExecuteScalar())
    End Using
End Sub

现在不是那么整洁吗?