プロが教えるわが家の防犯対策術!

同一プロ―シージャ内で連続して(実際には4つの)別テーブルにselect文を発行したいのですが、
1回目は正常に動作するのですが、どしても2回目のcommand.ExecuteReader()でエラーが発生してします。
皆さんこういう場合、どうされているのでしょうか?
(テーブル数が多いのでjoinで繋ぐとあまりにも複雑SQLになるのでこの様にしています。)
開発環境:VisualStudio2015
言語:VB


Using cn As SqlConnection = New SqlConnection()

cn.ConnectionString = "…接続文字列…"
cn.Open()

Dim command As New SqlClient.SqlCommand
command.Connection = cn
command.CommandType = CommandType.Text
command.CommandText = "SELECT … FROM tableA WHERE …"

'SQLの結果を取得する
Dim sr As SqlClient.SqlDataReader
sr = command.ExecuteReader()

If IsNothing(sr) Then
'データ無し
Else
'有り
While sr.Read
…処理…
End While
End If

End Using


Using cn As SqlConnection = New SqlConnection()

cn.ConnectionString = "…接続文字列…"
cn.Open()

Dim command As New SqlClient.SqlCommand
command.Connection = cn
command.CommandType = CommandType.Text
command.CommandText = "SELECT … FROM tableB WHERE …"

Dim sr As SqlClient.SqlDataReader
sr = command.ExecuteReader()   ’←ここでエラー発生!!!!!!!!!

If IsNothing(sr) Then
'データ無し
Else
'有り
While sr.Read
…処理…
End While
End If

End Using

A 回答 (1件)

具体的なエラーは何ですか?


2番目のSQLが単に間違っているとかはないですか?
    • good
    • 0
この回答へのお礼

ありがとございました。
SQLには問題なかったのですが、DBに接続する際のUserにSelect権限の無いtableを見ようとしていました。
お騒がせ致しました。

また、データの有無を確認する際、
 If IsNothing(sr) Then
としていましたが、
うまく動作しなかったので、
 If sr.HasRows Then
として、目出度く通りました。

お礼日時:2017/10/02 12:03

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

関連するカテゴリからQ&Aを探す