我想做一個非常簡單的程序,使用VB.NET連接到MySQL數據庫。我的程序只有一個表單和一個標籤。我設置我的MySQL數據庫,使其具有一個表格和一個名爲「Tab1」的字段(一個VARCHAR)使用VB.NET連接到MySQL
。我使用PHPMyAdmin手動將值「CLOSED」插入到Tab1字段中。我希望我的程序將字段的值更改爲「打開/關閉」,並且我還希望我的表單上的標籤文本可以在點擊時更改爲
。
到目前爲止,我已經添加了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
我製作的節目連接到MySQL上的Form_Load這樣的:
Connect("db4free.net", "boomun", "*******", "boomdb")
,並將其連接正常,但這裏是我的麻煩....我希望字段從關閉更改時打開我點擊Label1。它的確如此,但它在第二次點擊時不會改變。不知何故,我需要一個工作代碼來更新我的表中的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
你是怎麼叫'UPDATE'語句的?看起來你只是在評論中有一個sql語句... – mellamokb 2011-02-26 22:57:33
它只是一個評論,因爲它不工作....我問我應該做什麼,而是..這就是我的問題。 – BooMGiRL 2011-02-27 00:10:44