简体   繁体   English

在vb.net中更新SQL语句

[英]Update SQL statement in vb.net

I am new in VB.NET and as well as SQL. 我是VB.NET和SQL的新手。 I want to update records in my database. 我想更新数据库中的记录。 I made a dummy in my database. 我在数据库中做了一个假人。

Example the values are: ID = 1, name=Cath, age=21 示例值包括:ID = 1,name = Cath,age = 21

In my interface made in VB.NET, I update the values example : name = txtName.Text and age = txtAge.Text where ID = 1. This is in my main form. 在VB.NET中创建的界面中,我更新值示例:name = txtName.Text和age = txtAge.Text,其中ID =1。这是我的主要形式。 In my main form, I have "view" button informing that by clicking that button, new form would pop up and would view the updated values by the user. 在我的主表单中,我有“查看”按钮,通知该用户单击该按钮将弹出新表单,并由用户查看更新的值。 The program does not have any errors except that when I want to update again the values in my SQL, It record BUT when I click "view" button again, It will show the previous inputted by the user (the first update upon running the interface). 该程序没有任何错误,只不过当我想再次更新SQL中的值时,它会在我再次单击“查看”按钮时记录BUT,它将显示用户先前输入的内容(运行界面时的第一次更新) )。 What should be the solution? 应该怎么解决?

This is my code: 这是我的代码:

Mainform: 主要形式:

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

Form 2: (where the updated data would be viewed) 表格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

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

更新sql时,连接字符串保持打开状态,因此当您尝试检索数据时,条件会关闭连接而不读取数据或更新文本框。

Take out the .ExecuteNonQuery() from the ViewInfos() method. 从ViewInfos()方法中取出.ExecuteNonQuery()。

Refer to your forms via variables not via the form name: 通过变量而不是通过表单名称来引用表单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM