實現此目的的最佳方法是重新加載數據網格視圖。這裏是解釋如何做的代碼
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()
Sql注入警報。你的代碼實際上是乞求被黑客攻擊。 – 2013-02-12 18:53:55
快速示例:嘗試放置文本:'';刪除表分支; - 在您的街道地址字段中......或者如果您不想從備份中恢復該表,請不要。 – 2013-02-12 18:55:33
謝謝先生。但是,你能幫我解決我遇到的更新問題嗎? – tristan 2013-02-12 19:02:03