質問編集履歴

1

追記

2017/07/30 09:26

投稿

chintao1224
chintao1224

スコア155

test CHANGED
File without changes
test CHANGED
@@ -105,3 +105,83 @@
105
105
 
106
106
 
107
107
  よろしくお願いします。
108
+
109
+
110
+
111
+ 教えていただいた情報を読みコードを書いてみました。
112
+
113
+
114
+
115
+ ```vb.net
116
+
117
+ Using cn As NpgsqlConnection = New NpgsqlConnection()
118
+
119
+ cn.ConnectionString = 接続文字列
120
+
121
+ cn.Open()
122
+
123
+ Dim strSelectSQL As String = セレクト文
124
+
125
+ Dim nSelectCmd As NpgsqlCommand = New NpgsqlCommand(strSelectSQL, cn)
126
+
127
+ Dim dr As NpgsqlDataReader = nSelectCmd.ExecuteReader
128
+
129
+ If (dr.HasRows = False) Then
130
+
131
+ 'Dim trn As NpgsqlTransaction = cn.BeginTransaction()
132
+
133
+ Try
134
+
135
+ Dim strInsertSQL As String = インサート文
136
+
137
+ Dim nInsertCmd As NpgsqlCommand = New NpgsqlCommand(strInsertSQL, cn)
138
+
139
+ Dim intCount As Int32 = nInsertCmd.ExecuteNonQuery()
140
+
141
+ 'trn.Commit()
142
+
143
+ nInsertCmd.Dispose()
144
+
145
+ Catch ex As Exception
146
+
147
+ 'trn.Rollback()
148
+
149
+ Dim strDateTime As String = Format(Now, "yyyy-MM-dd hh:mm:ss")
150
+
151
+ Call sWriteErrorLog(strDateTime, "DB選択", ex.ToString)
152
+
153
+ Finally
154
+
155
+ dr.Dispose()
156
+
157
+ nSelectCmd.Dispose()
158
+
159
+ End Try
160
+
161
+ End If
162
+
163
+ End Using
164
+
165
+ ```
166
+
167
+
168
+
169
+ コメントアウトしていますが、Dim trn As NpgsqlTransaction = cn.BeginTransaction()でエラーになります。
170
+
171
+
172
+
173
+ またDim intCount As Int32 = nInsertCmd.ExecuteNonQuery()でcatchに行き以下のようなエラーが出ます。
174
+
175
+
176
+
177
+ 「"A command is already in progress: セレクトのSQL文"」
178
+
179
+
180
+
181
+ vb.netが初心者に近いので解らないのですが。
182
+
183
+
184
+
185
+ 何が良くないのでしょうか?
186
+
187
+ なぜトランザクションがエラーになるのでしょうか?