0

这是我的代码:

        Dim sqledit As New SqlCommand
        sqledit = New SqlCommand("UPDATE Branches SET StAddress='" + txtstaddress.Text + "',City='" + txtcity.Text + "',Province='" + txtprovince.Text + "',ContactNo='" + txtcontact.Text + "' WHERE BranchID='" + txtbranchid.Text + "'", con1)
        sqledit.ExecuteNonQuery()
        MsgBox("Branch Information Updated!", MsgBoxStyle.Information, Title:="Edit Branch Information")
        utilities.Refresh()
        Me.Close()

即使我刷新它,我也无法更新它。请帮忙!

4

2 回答 2

1

也有一种简单的方法,您可以通过调用所有表格内容示例的 datagridview 进行查询:

SELECT {your table field}
FROM {table name}

如果您在更改后执行此查询,它将向您显示 DGV 上的新数据。

于 2014-01-25T11:29:58.240 回答
0

实现这一点的最佳方法是重新加载数据网格视图。这是解释如何执行此操作的代码

Private Sub refreshDatagrid()
    Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=reerp;User ID=root;Password=root;")
    Dim sql As MySqlCommand = New MySqlCommand("SELECT * FROM users", con)
    Dim ds As DataSet = New DataSet()
    Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
    con.Open()
    DataAdapter1.SelectCommand = sql
    DataAdapter1.Fill(ds, "Users")
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "Users"
    con.Close()

End Sub

现在,一旦您将数据添加到网格中,然后再次调用此方法。这是你如何做的例子

   Dim query As String
    Dim con As MySqlConnection = New MySqlConnection("data source=localhost;database=reerp;user id=root;password=root;")

    query = "insert into users (username,password,first_name,last_name,role) values('"
    query = query + tb_username.Text + "','" + tb_password.Text + "','" + tb_firstname.Text + "','" + tb_lastname.Text + "','" + ComboBox_role.Text + "');"
    con.Open()

    Dim cmd As MySqlCommand = New MySqlCommand(query, con)

    Dim i As Integer = cmd.ExecuteNonQuery()
    If (i > 0) Then
        lblMsg.Text = "record is successfully inserted"
        refreshDatagrid()

    Else
        lblMsg.Text = "record is not inserted"
    End If
    con.Close()
于 2013-08-21T10:15:43.863 回答