我想更新并在vb.net
问题描述:
'Adding Function
Private Sub AddCustomer()
Try
sql = "INSERT INTO Category(catid, caname) Values('" & TextBox1.Text & "', '" & TextBox2.Text & "')"
ConnD()
cmd = New MySqlCommand(sql, conn1)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Customer Added", MsgBoxStyle.Information, "Add Customer")
Else
MsgBox("Failed to add customer", MsgBoxStyle.Critical, "Add Customer")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn1.Close()
End Try
End Sub
'UpdateFunction
Private Sub UpdateCustomer()
Try
sql = "Update category set caname ='" & TextBox2.Text & "' where catid = '" & TextBox1.Text & "' "
ConnD()
cmd = New MySqlCommand(sql, conn1)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Customer Updated", MsgBoxStyle.Information, "Update Customer")
Else
MsgBox("Failed to update customer", MsgBoxStyle.Critical, "Update Customer")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn1.Close()
End Try
End Sub
我曾尝试使用添加添加使用一个按键记录和编辑使用一个按钮。我想更新并在vb.net
我写下面的按钮编码。
只有更新部件的作品。为什么不是加部分工作?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Len(TextBox1.Text) > 0 Then
UpdateCustomer()
Else
AddCustomer()
End If
End Sub
答
您可以更新在运行时Button1的文本,并根据您可以更新或新增客户Button1的文本..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Update" Then
UpdateCustomer()
ElseIf Button1.Text = "Save"
AddCustomer()
End If
End Sub
你需要做的第一件事就是阅读有关SQL注入和参数查询。 –
您的Textbox1文本长度始终大于零,无论其更新或保存。所以检查按钮文字... – PRABA
不要忘记看看SQL注入。 –