簡體   English   中英

在vb.net中更新SQL語句

[英]Update SQL statement in vb.net

我是VB.NET和SQL的新手。 我想更新數據庫中的記錄。 我在數據庫中做了一個假人。

示例值包括:ID = 1,name = Cath,age = 21

在VB.NET中創建的界面中,我更新值示例:name = txtName.Text和age = txtAge.Text,其中ID =1。這是我的主要形式。 在我的主表單中,我有“查看”按鈕,通知該用戶單擊該按鈕將彈出新表單,並由用戶查看更新的值。 該程序沒有任何錯誤,只不過當我想再次更新SQL中的值時,它會在我再次單擊“查看”按鈕時記錄BUT,它將顯示用戶先前輸入的內容(運行界面時的第一次更新) )。 應該怎么解決?

這是我的代碼:

主要形式:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SQLConnection.ConnectionString = ServerString
    Try
        If SQLConnection.State = ConnectionState.Closed Then
            SQLConnection.Open()
            MessageBox.Show("Successful connection")
        Else
            'SQLConnection.Close()
            MessageBox.Show("Connection is closed")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try

End Sub

 Public Sub SaveNames(ByRef SQLStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand
 With cmd
        .CommandText = SQLStatement
        .CommandType = CommandType.Text
        .Connection = SQLConnection
        .ExecuteNonQuery()
    End With

    MsgBox("Successfully Added!")

End Sub

 Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    Dim date_now As String

    date_now = Format(dtpDate.Value, "yyyy-MM-dd")

    Dim SQLStatement As String = "UPDATE people SET name='" & txtName.Text & "', date ='" & date_now & "'  WHERE 1"
    SaveNames(SQLStatement)
End Sub

表格2 :(將在其中查看更新的數據)

 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SQLConnection.ConnectionString = ServerString
    Try
        If SQLConnection.State = ConnectionState.Closed Then
            SQLConnection.Open()
            '====retrieve / update values in database=============
            Dim SQLStatement As String = "SELECT name, date FROM people"
            ViewInfos(SQLStatement)
        Else
            'SQLConnection.Close()
            MessageBox.Show("Connection is closed")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try
End Sub

 Public Sub ViewInfos(ByRef SQLStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd
        .CommandText = SQLStatement
        .CommandType = CommandType.Text
        .Connection = SQLConnection
        .ExecuteNonQuery()

    End With
    '--read the records in database in phpmyadmin gui---
    Dim myReader As MySqlDataReader = cmd.ExecuteReader

    If myReader.Read Then
        lblName.Text = myReader.GetString(0)
        lblDate.Text = myReader.GetString(1)

    End If

    myReader.Close()
    MsgBox("Records Successfully Retrieved")

End Sub

任何幫助,將不勝感激。 謝謝!

更新sql時,連接字符串保持打開狀態,因此當您嘗試檢索數據時,條件會關閉連接而不讀取數據或更新文本框。

從ViewInfos()方法中取出.ExecuteNonQuery()。

通過變量而不是通過表單名稱來引用表單:

Dim myForm as New Form2()
myForm.Show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM