sql-server - 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"))
解决方案
希望这会有所帮助
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
推荐阅读
- c# - 为什么我的应用程序在出现未处理的异常后不会停止?
- node.js - 如何在 ejs 中获取数据的索引之间切换?
- python - 清理未命名:0,未命名:1索引列函数
- python - 从对应于月日列表的熊猫数据框中提取行
- typescript - 如何在现有但当前未知的键/值对上设置接口?
- django - 如何将搜索结果传递给会话。结果可能是一个对象或许多对象
- powershell - 如何仅使用 Powershell 在 IE 浏览器中检查服务器是否已启动并运行
- vue.js - 我们可以修改 Vuex 操作以进行开玩笑的单元测试吗?
- android - ConstraintLayout 在 DialogFragment 边界之外绘制
- excel - 有没有办法按多个标准分组然后计算数据?