我正在使用appg创建,删除和更新表中的记录,使用datagridview来显示数据......
我可以创建文本框中的新记录并将其保存到sql表行......但是在更新和删除记录方面我似乎很难...
我想知道如何从DGV中删除所选行并将其从表中删除。
更新所选行,将列数据加载到文本框,并能够更新并保存新值......
请帮助我..这是迄今为止的结果..
UPDATE
按钮的代码:
Private Sub BtnModificar_Click(sender As Object, e As EventArgs) Handles BtnModificar.Click
Using con As New SqlConnection("Data Source=LUISFFER-PC\SQLEXPRESS;Initial Catalog=Mendez;Persist Security Info=True;User ID=Invitado;Password=123456;")
con.Open()
'The app is in spanish, so please feel free to translate it, or write it in english
If TxtNombre.Text = "" Then
'Si el nombre esta vacio,
MsgBox("No hay dato para modificar") 'There is no record to update
'se muestra este mensaje.
TxtCodigo.Focus()
'Y devuelve el foco a Txtcodigo.
Exit Sub
End If
BloquearTexBox(True)
BloquearBotones(False)
BtnGuardar.Enabled = True
BtnCancelar.Enabled = True
Panel2.Enabled = True
Modificar = True
End Using
End Sub
DELETE
按钮的代码:
Private Sub BtnEliminar_Click(sender As Object, e As EventArgs) Handles BtnEliminar.Click
If MessageBox.Show("Esta seguro que desea eliminar este registro", "Eliminar Registro", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Using conec As New SqlConnection("Data Source=LUISFFER-PC\SQLEXPRESS;Initial Catalog=Mendez;Persist Security Info=True;User ID=Invitado;Password=123456")
conec.Open()
Dim Comando As New SqlCommand("Delete From Alumnos Where ID=@ID", conec)
Comando.Parameters.Add("@ID", SqlDbType.Int, 10).Value = Val(TxtCodigo.Text)
Comando.ExecuteNonQuery()
CargarDataGrid()
Exit Sub
End Using
Else
Exit Sub
End If
End Sub
答案 0 :(得分:0)
看起来它与您如何设置参数有关。请尝试使用AddWithValue或用您的值替换10
。您正在将值从10更改为文本框的值,但仅在参数已添加到命令后才会更改。
方法1:AddWithValue
Comando.Parameters.AddWithValue("@ID", Integer.Parse(TxtCodigo.Text))
方法2:在参数
中添加值Comando.Parameters.Add("@ID", SqlDbType.Int, Integer.Parse(TxtCodigo.Text))