使用文本框更新和删除SQL上的记录

时间:2017-01-02 21:22:55

标签: sql-server vb.net datagridview sql-update

我想要更新和删除记录,我使用DataGridView和Textbox进行查询,所以从我删除记录的位置并不重要,我只是不知道该怎么做,下面是我用来创建新记录的代码,所以也许如果我修改了一些东西可以用于更新和删除,但它可以是什么?,我知道它只是一个问题,但我不记得该怎么办它

    Private Sub btnSaveRecord_Click(sender As Object, e As EventArgs) Handles btnSaveRecord.Click

    Dim Conn As New SqlClient.SqlConnection("Server=192.168.1.104,1433;" + "Database=Base1;Uid=Admindb;Password=2016")   
    Dim msg As String
    Dim title As String
    Dim style As MsgBoxStyle
    Dim response As MsgBoxResult

    msg = "No puede haber ningun campo vacio"

    style = MsgBoxStyle.DefaultButton2 Or _
       MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly

    title = "Error"

    Dim sql As String = "Insert into Table3 values (@Nombre, @Paterno, @Materno, @Colonia, @Delegacion, @Estado, @Telefono1, @Telefono2, @Telefono3, @Comentario1, @Comentario2, @Comentario3, @Referencia, @Nombres, @Domicilio, @Codigopostal, @Origen)"
    Dim cmd As New SqlCommand(sql, Conn)
    tbConcatenado.Text = tbName.Text + " " + tbPaterno.Text + " " + tbMaterno.Text
    Try
        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add("@Nombre", SqlDbType.NVarChar, 25).Value = Me.tbName.Text
        cmd.Parameters.Add("@Paterno", SqlDbType.NVarChar, 25).Value = Me.tbMaterno.Text
        cmd.Parameters.Add("@Materno", SqlDbType.NVarChar, 25).Value = Me.tbPaterno.Text
        cmd.Parameters.Add("@Colonia", SqlDbType.NVarChar, 25).Value = Me.tbColony.Text
        cmd.Parameters.Add("@Delegacion", SqlDbType.NVarChar, 25).Value = Me.tbDelegacion.Text
        cmd.Parameters.Add("@Estado", SqlDbType.NVarChar, 25).Value = Me.tbEstado.Text
        cmd.Parameters.Add("@Telefono1", SqlDbType.NVarChar, 50).Value = Me.tbTel1.Text
        cmd.Parameters.Add("@Telefono2", SqlDbType.NVarChar, 50).Value = Me.tbTel2.Text
        cmd.Parameters.Add("@Telefono3", SqlDbType.NVarChar, 50).Value = Me.tbTel3.Text
        cmd.Parameters.Add("@Comentario1", SqlDbType.NVarChar, 50).Value = Me.tbCom1.Text
        cmd.Parameters.Add("@Comentario2", SqlDbType.NVarChar, 50).Value = Me.tbCom2.Text
        cmd.Parameters.Add("@Comentario3", SqlDbType.NVarChar, 50).Value = Me.tbCom3.Text
        cmd.Parameters.Add("@Referencia", SqlDbType.NVarChar, 50).Value = Me.tbReferencia.Text
        cmd.Parameters.Add("@Nombres", SqlDbType.NVarChar, 100).Value = Me.tbConcatenado.Text
        cmd.Parameters.Add("@Domicilio", SqlDbType.NVarChar, 50).Value = Me.tbAdress.Text
        cmd.Parameters.Add("@Codigopostal", SqlDbType.NVarChar, 50).Value = Me.tbZipCode.Text
        cmd.Parameters.Add("@Origen", SqlDbType.NVarChar, 25).Value = Me.tbOrigen.Text


        If tbName.Text = "" Or
            tbMaterno.Text = "" Or
            tbPaterno.Text = "" Or
            tbColony.Text = "" Or
            tbDelegacion.Text = "" Or
            tbEstado.Text = "" Or
            tbTel1.Text = "" Or
            tbReferencia.Text = "" Or
            tbAdress.Text = "" Or
            tbZipCode.Text = "" Or
            tbOrigen.Text = "" Then
            response = MsgBox(msg, style, title)
        Else

            Conn.Open()
            cmd.ExecuteNonQuery()

            MessageBox.Show("New Record Updated")
            Me.Close()
        End If

    Catch ex As Exception

    End Try


End Sub

0 个答案:

没有答案