首页 > 解决方案 > Visual Studio 2017 - 通过按钮删除 SQL 数据库行

问题描述

非常感谢premiumbetine,我现在可以从组合框中选择一个名称,该名称将在TextBoxes中显示其他列条目,但现在我正在尝试弄清楚如何从数据库中删除该行并立即在ComboBox中更新它单击 Button1,我当前的代码什么也不做(对于 Button1)。

Public Class Form1
Dim dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim con As New SqlConnection("Data Source=xxxx\sqlexpress;Initial Catalog=ltLeavers;Integrated Security=True")

    Dim da As New SqlDataAdapter("SELECT * FROM dbo.ltData", con)
    da.Fill(dt)
    ComboBox1.DisplayMember = "DISPLAY_NAME"
    ComboBox1.DataSource = dt

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim con As New SqlConnection("Data Source=xxxxx\sqlexpress;Initial Catalog=ltLeavers;Integrated Security=True")
    Dim del As New SqlDataAdapter("DELETE * FROM dbo.ltData where DISPLAY_NAME='" & ComboBox1.SelectedIndex & "'", con)

    MsgBox("Thank your for your submission.", MsgBoxStyle.OkOnly, "Success!")

End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    TextBox1.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("EMAIL_ADDRESS"))
    TextBox3.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("OFFICE"))
    TextBox2.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("DEPARTMENT"))
    TextBox12.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("NEG_ID"))

标签: sql-servervb.net

解决方案


希望这会有所帮助

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim con As New SqlConnection("Data Source=xxxxx\sqlexpress;Initial Catalog=ltLeavers;Integrated Security=True")
    con.Open()

    strSQL = "DELETE * FROM dbo.ltData where DISPLAY_NAME='" & ComboBox1.SelectedText & "'"

    Dim com As New SqlCommand(strSQL, con)

    com.ExecuteNonQuery()

    MsgBox("Thank your for your submission.", MsgBoxStyle.OkOnly, "Success!")
End Sub

推荐阅读