2011-12-11 79 views
1

StudentDetails.Students是與RegistrationDetails.Registration形成關係的主表。因此,StudentID是前者的主鍵,而後者是外鍵。如何從兩個表中刪除相關記錄的內容?

現在我嘗試以下每個碼但每個前兩個給出錯誤信息「附近有語法錯誤‘A’」,並在中的第三個DbTransaction的情況一樣「DIM反式作爲DbTransaction」是也不是有效的類型。請使用SQL Server 2008專業版。

1.

cmd = New SqlCommand("DELETE FROM StudentDetails.Students a, RegistrationDetails.Registration b WHERE (b.StudentId=a.StudentId) AND a.StudentId='" & txtStudentID.Text & "'", cn) 

2.

cmd = New SqlCommand("DELETE FROM StudentDetails.Students a, RegistrationDetails.Registration b WHERE (b.StudentId=a.StudentId) AND a.StudentId='/" & txtStudentID.Text & "/'", cn) 
+0

您是否試圖根據學生ID從每個表中刪除一行? – Chris

+0

是的,我試圖從主表上刪除基於StudentID的記錄。 – Akaglo

+0

儘管在這種情況下可能並不重要,但是請記住,當您使用'「&txtStudentID.Text&」'代替SQL參數時,您打開了自己的SQL注入功能 – Seph

回答

2

我真的不知道有關.NET任何東西,但你應該嘗試這樣的:

cmd = New SqlCommand("Delete from StudentDetails.Students a, RegistrationDetails.Registration b where (b.StudentId=a.StudentId) and a.StudentId='" & txtStudentID.Text & "'", cn) 

因爲你刪除整個記錄,而不是列,這就是爲什麼你不這樣做,也不能指定DELETE子句中的列名稱

+0

這會給出錯誤消息「a'附近的語法不正確 – Akaglo

+0

代碼給出錯誤消息「a'附近的語法不正確」 – Akaglo

3

刪除時不要指定領域,同時從1個表中刪除,並在交易中換行的一切:

Dim trans As SqlTransaction 

trans = cn.BeginTransaction 

Try 
    Dim cmd1 As New SqlCommand("Delete from Registration where StudentId='" & txtStudentID.Text & "'", cn) 

    cmd1.ExecuteNonQuery() 

    Dim cmd2 As New SqlCommand("Delete from StudentDetails where StudentId='" & txtStudentID.Text & "'", cn) 

    cmd2.ExecuteNonQuery() 

    trans.Commit() 
Catch theException As Exception 
    ' Report the exception 
    trans.Rollback() 
End Try 
+0

DbTransaction與「Dim trans As DbTransaction」中的DbTransaction不是有效的類型。請使用SQL Server 2008專業版。 – Akaglo

+0

請DbTransaction被視爲無效類型。我正在使用VB 2008專業版。 – Akaglo

+0

@Akaglo:您可以將類型更改爲SqlTransaction –

1

首先刪除相關規定StudentId「登記」表中的所有記錄。

然後,從主表格「StudentDetails」中刪除它。

爲了保持數據的一致性,請使用事務。

+0

請通過修改我的代碼準確顯示我應該做什麼。 – Akaglo

0

通常最好使用事務,特別是從兩個表中刪除相關數據時。使用參數化而不是級聯並閱讀有關SQL injection的更多信息。

您的代碼應該看起來更像是這樣的:

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.VarChar).Value = txtStudentID.Text 
       cmd.ExecuteNonQuery() 

       cmd.CommandText = "delete from StudentDetails.Students where StudentId = @studentId" 
       cmd.Parameters.Add("@studentId", SqlDbType.VarChar).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 

我認爲StudentIdvarchar類型(因爲使用單引號的:a.StudentId='" & txtStudentID.Text & "'"),如果它不是SqlDbType.VarChar改變不管它是什麼。

+0

我按照用於StudentID的數據類型將SqlDbType.VarChar更改爲SqlDbType.BigInt,但不斷提出以下錯誤消息。 「初始化字符串的格式不符合從索引0開始的規範。」 – Akaglo

+0

@Akaglo:你可以更新問題並粘貼更多代碼(執行SqlCommand的上下文)。 –