2011-12-11 8 views
1

StudentDetails.StudentsRegistrationDetails.Registrationとの関係を形成するマスターテーブルです。したがって、StudentIDは前者では主キーですが、後者では外部キーです。2つのテーブルから関連するレコードの内容を削除する方法はありますか?

私は次の各コードを試しましたが、最初の2つはそれぞれ「不正確な構文 'a'」と3番目のもののエラーメッセージ "Dim trans As DbTransaction"のようにDbTransactionまた、有効なタイプでもありません。私はSQL Server 2008 Professional Editionを使用しています。あなたので

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

:私は本当に.NETへの関連は何も知りませんが、あなたのようなものを試してみてください

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

この場合は重要ではありませんが、SQLパラメータの代わりに ''&txtStudentID.Text& "'を使用すると、SQLインジェクションのために自分自身を開いていることに注意してください。 – Seph

答えて

2

列ではなくレコード全体を削除します。そのため、DELETE句に列名を指定したり指定したりできません。

+0

これは、 – 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

「Dim trans As DbTransaction」のようなDbTransactionは有効な型ではありません。私はSQL Server 2008 Professional Editionを使用しています。 – Akaglo

+0

DbTransactionが無効な型として表示されています。私はVB 2008プロフェッショナル版を使用しています。 – Akaglo

+0

@Akaglo:タイプをSqlTransactionに変更することができます –

1

最初に指定されたStudentIdに関連し、「登録」テーブル内のすべてのレコードを削除します。

次に、マスターテーブル "StudentDetails"から削除します。

データの一貫性を保つため、トランザクションを使用してください。

+0

私のコードを変更することで何をすべきか正確に教えてください。 – Akaglo

0

通常、特に2つのテーブルから関連するデータを削除する場合は、トランザクションを使用する方がよいでしょう。連結の代わりにパラメータ化を使用し、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 

私はStudentId列が(理由にアポストロフィを使用する:a.StudentId='" & txtStudentID.Text & "'"varcharタイプであることを前提とし、それはそれが何であれにSqlDbType.VarCharを変更しないなら、。

+0

StudentIDに使用されるデータ型に合わせてSqlDbType.VarCharをSqlDbType.BigIntに変更しましたが、次のエラーメッセージが常に発生しています。 "初期化文字列の書式がインデックス0から始まる指定に適合しません。 – Akaglo

+0

@Akaglo:質問を更新し、より多くのコード(SqlCommandを実行しているコンテキスト)を貼り付けることができます。 –

関連する問題