【问题标题】:Deleting a record from multiple tables从多个表中删除一条记录
【发布时间】:2013-01-30 02:36:07
【问题描述】:

已为每个科目(例如英语语言、数学)创建了一个表,并且学生表与每个科目表相关,如果有人可以编辑下面的代码以使我能够执行删除命令,我将不胜感激从这些多个表中删除一条记录。

一个重要的问题是应该有一种方法来执行删除命令,这样相关记录也可以从任何其他主题表中删除,这些记录将在未来引入新主题时创建。

Dim cd As String

If txtName.Text = "" And cboDay.Text = "" And cboMonth.Text = "" And txtYear.Text = "" And lblAge.Text = "" And radioMale.Checked = False Or RadioFemale.Checked = False And txtGName.Text = "" And txtMPhone.Text = "" And txtEmail.Text = "" And txtAddress.Text = "" And txtCity.Text = "" And cboRegion.Text = "" And PictureBox1.ImageLocation = "" Then
    MessageBox.Show("There is no record selected to delete. Search for the record to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
    cd = MessageBox.Show("You are about to delete this record. Are you sure you want to delete?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If cd = vbYes Then
        cmd = New SqlCommand("Delete from StudentDetails.Registration where StudentId='" & txtStudentId.Text & "'", cn)
        cmd.ExecuteNonQuery()

        cmd = New SqlCommand("Delete from StudentDetails.Students where StudentId='" & txtStudentId.Text & "'", cn)
        cmd.ExecuteNonQuery()

        cmd = New SqlCommand("Delete from ProgramDetails.EnglishLanguage where StudentId='" & txtStudentId.Text & "'", cn)
        cmd.ExecuteNonQuery()

        MessageBox.Show("Record deleted", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Showgrid()
        txtStudentId.Clear()
        txtName.Clear()
        cboDay.Text = ""
        cboMonth.Text = ""
        lblAge.Text = ""
        txtNationality.Clear()
        If radioMale.Checked = True Then
            Sex = ""
        End If
        cboStudentType.Text = ""
        cboHouse.Text = ""
        cboRoom.Text = ""
        txtGName.Clear()
        txtMPhone.Clear()
        txtHPhone.Clear()
        txtEmail.Clear()
        txtAddress.Clear()
        txtCity.Clear()
        cboRegion.Text = ""
        PictureBox1.Image = PictureBox1.ErrorImage
        txtStudentId.Focus()
    End If
End If

【问题讨论】:

    标签: sql-server vb.net winforms tsql ado.net


    【解决方案1】:

    你为什么不试试DELETE CASCADE。它比在代码中手动执行要好。

    通过使用级联参照完整性约束,您可以定义 当用户尝试删除或删除 SQL Server 时执行的操作 更新现有外键指向的键。

    关于删除级联 指定如果尝试删除行 与其他现有行中的外键引用的键 表,所有包含这些外键的行也会被删除。

    至于你提供的代码,命令应该是这样的:

    cmd = New SqlCommand("Delete from StudentDetails.Registration where StudentId=" & Integer.Parse(txtStudentId.Text), cn)
    

    虽然您应该使用参数化查询来避免 Sql Injection:

    cmd = New SqlCommand("Delete from StudentDetails.Registration where StudentId = @StudentId" , cn)
    cmd.Parameters.AddWithValue("@StudentId", Integer.Parse(txtStudentId.Text))
    

    【讨论】:

    • @Akaglo 更新了我的答案。
    • 感谢您对SQL注入的好建议,但现在主要有两个问题。 1. 我想让你告诉我如何在 SQL Server 中使用 CASCADE 方法和 2. 我如何通过变量或某种东西来表示所有主题,就好像所有主题表都是一个表一样。因为它不会是适合写 ProgramDetails.EnglishLanguage、ProgramDetails.Mathematics、ProgramDetails.French 等内容
    • @Akaglo 删除级联非常简单,查看this tutorial。您只需在所有相关表中为 StudentId 引用设置ON DELETE CASCADE。
    • @Akaglo 第二点我没看懂,如果我理解正确的话,如果它们都在同一个表中,则不需要指定主题。
    • @Akaglo 还检查this answer 进行级联删除。
    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多