使用VB.NET连接到MySQL

5m1hhzi4  于 2023-01-01  发布在  Mysql
关注(0)|答案(3)|浏览(253)

我正在尝试制作一个使用VB.NET连接到MySQL数据库的非常简单的程序。我的程序只有一个窗体和一个标签。我设置了MySQL数据库,使其具有一个表和一个字段(VARCHAR)
名为"Tab1"。我使用PHPMyAdmin手动将值"CLOSED"插入Tab1字段。我希望我的程序将该字段的值更改为OPEN/CLOSED,并且我还希望窗体上的标签文本
点击时也会改变。
So far, I've added the MySQL.data reference to my project and added:

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

致将军--宣言
我还声明了一个包含MySQL Connection的全局变量:

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

那更新行是不是为我工作...可以请任何人提供一个工作代码?谢谢我真的很感激!!
下面是该项目的全部代码:

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
jckbn6z7

jckbn6z71#

请尝试以下语句:

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

当然,使用ExecuteNonQuery方法来执行语句。如果你的意思是OPEN和CLOSE是变量,那么使用Parameters来代替(这样更安全)。

c2e8gylq

c2e8gylq2#

像这样...

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

???

14ifxucb

14ifxucb3#

这里的一个错误是使用全局连接对象。.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

现在不是更整洁了吗?

相关问题