[英]Connecting to MySQL using VB.NET
I am trying to make a very simple program that connects to a MySQL database using VB.NET.我正在尝试制作一个非常简单的程序,它使用 VB.NET 连接到 MySQL 数据库。 My program only has one form and one label.
我的程序只有一种形式和一种标签。 I setup my MySQL database to have a table and one field (a VARCHAR)
我将 MySQL 数据库设置为有一个表和一个字段(一个 VARCHAR)
called "Tab1".称为“Tab1”。 I manually inserted the value "CLOSED" into the Tab1 field using PHPMyAdmin.
我使用 PHPMyAdmin 手动将值“CLOSED”插入到 Tab1 字段中。 I want my program to change the value of the field to OPEN/CLOSED and I also want the label text on my form to
我希望我的程序将该字段的值更改为 OPEN/CLOSED 并且我还希望表单上的标签文本
change too when it's clicked.单击时也会更改。
So far, I've added the MySQL.data reference to my project and added:到目前为止,我已经在我的项目中添加了 MySQL.data 引用并添加了:
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
To the General_Declarations致 General_Declarations
Also I declared a global variable containing a MySQL Connection:我还声明了一个包含 MySQL 连接的全局变量:
Public Class Form1
Dim conn As New MySqlConnection
Here is my function that connects to MySQL:这是我连接到 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
I made the program connect to MySQL on Form_Load like this:我让程序在 Form_Load 上连接到 MySQL,如下所示:
Connect("db4free.net", "boomun", "*******", "boomdb")
And it connects fine but here is where I have the trouble.... I want the field to change from CLOSED to OPEN when I click Label1.它连接良好,但这里是我遇到麻烦的地方......我希望当我单击 Label1 时该字段从 CLOSED 更改为 OPEN。 It does, but it doesn't change back on the second click.
它确实如此,但它不会在第二次单击时变回。 Somehow I need a working code to Update the Tab1 field in my table.
不知何故,我需要一个工作代码来更新表中的 Tab1 字段。
Here is what I have so far:这是我到目前为止所拥有的:
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
That UPDATE line isn't working for me... Can anyone please provide a working code?那条 UPDATE 行对我不起作用......任何人都可以提供一个有效的代码吗? Thanks I really appreciate it!!
谢谢我真的很感激!!
Here is the entire code of the project all together:这是项目的全部代码:
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
Try the following statement instead:请改为尝试以下语句:
UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED'
Ofcourse use a ExecuteNonQuery method to execute the statement.当然使用 ExecuteNonQuery 方法来执行语句。 If you meant that OPEN and CLOSE are variables, use Parameters for that instead (it's safer).
如果您的意思是 OPEN 和 CLOSE 是变量,请改用参数(这样更安全)。
Like this...像这样...
Else
Label1.Text = "OPEN"
SQL = "UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED'"
myCommand.CommandText = SQL
myCommand.ExecuteNonQuery()
myData.Close()
End If
??? ???
One mistake here is using a global connection object.这里的一个错误是使用全局连接对象。 .Net database connections use a feature called connection pooling, which means most of the time it's really better to create a new connection object for each call out to your database.
.Net 数据库连接使用一种称为连接池的功能,这意味着在大多数情况下,最好为每次调用数据库创建一个新的连接对象。 What you can do is use that global to keep your connection 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
But the bigger question here is why you're going to the database twice at all?但这里更大的问题是,为什么要两次访问数据库?
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
Now isn't that a whole lot neater?现在不是更整洁了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.