如何从两个表中删除相关记录的内容(使用SQLTransaction)?

时间:2011-12-15 12:18:40

标签: vb.net tsql

我得到"初始化字符串的格式不符合从索引0和#34开始的规范;使用以下代码时出错。这些命令应该从两个表中删除相关记录,即StudentDetails.Students作为主表,RegistrationDetails.Registration作为子表。

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
    Try
        Dim cd As String

        If txtStudentID.Text = "" And 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 txtPhone.Text = "" And txtEmail.Text = "" And txtAddress.Text = "" And txtTown.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
                Using c As New SqlConnection("connection string")
                    c.Open()
                    Using tx As SqlTransaction = c.BeginTransaction()
                        Try
                            Using cmd As SqlCommand = c.CreateCommand()
                                cmd.CommandText = "delete from RegistrationDetails.Registration where StudentId = @studentId"
                                cmd.Parameters.Add("@studentId", SqlDbType.BigInt).Value = txtStudentID.Text
                                cmd.ExecuteNonQuery()

                                cmd.CommandText = "delete from StudentDetails.Students where StudentId = @studentId"
                                cmd.Parameters.Add("@studentId", SqlDbType.BigInt).Value = txtStudentID.Text
                                cmd.ExecuteNonQuery()

                                tx.Commit()
                            End Using
                        Catch generatedExceptionName As Exception
                            tx.Rollback()
                            ' take care of exception here
                            Throw
                        End Try
                    End Using
                End Using
                MessageBox.Show("Record deleted", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information)
                showgrid()
                txtStudentID.Clear()
                txtName.Clear()
                cboDay.Text = ""
                cboMonth.Text = ""
                txtYear.Clear()
                lblAge.Text = ""
                If radioMale.Checked = True Then
                    Gender = ""
                End If
                txtGName.Clear()
                txtPhone.Clear()
                txtEmail.Clear()
                txtAddress.Clear()
                txtTown.Clear()
                cboRegion.Text = ""
                PictureBox1.Image = PictureBox1.ErrorImage
                txtStudentID.Focus()
            End If
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

尝试这种方式可以解决您的问题。

cmd.CommandText = "delete from RegistrationDetails.Registration where StudentId = @studentId"
Dim sqlParam as SqlParameter = cmd.Parameters.Add("@studentId", SqlDbType.BigInt)
sqlParam.Value = txtStudentID.Text
cmd.ExecuteNonQuery()

cmd.CommandText = "delete from StudentDetails.Students where StudentId = @studentId"
sqlParam.Value = txtStudentID.Text
cmd.ExecuteNonQuery()

实际上,您在每次添加相同参数时都使用单个SqlCommand进行查询。

希望这对你有所帮助。