1
다음 코드를 사용하여 "초기화 문자열의 형식이 인덱스 0에서 시작하는 사양을 준수하지 않습니다."라는 오류가 발생합니다. 명령은 두 개의 테이블, 즉 StudentDetails.Students를 마스터 테이블로, RegistrationDetails.Registration을 자식 테이블로 삭제하여 관련 레코드를 삭제합니다.SQLTransaction을 사용하여 두 테이블에서 관련 레코드의 내용을 삭제하는 방법은 무엇입니까?
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