質問するログイン新規登録

質問編集履歴

1

追記

2017/07/30 09:26

投稿

chintao1224
chintao1224

スコア156

title CHANGED
File without changes
body CHANGED
@@ -51,4 +51,44 @@
51
51
 
52
52
  ちなみにselect文とinsert文はツールのA5:SQL Mk-2で実行して問題なく実行できます。
53
53
 
54
- よろしくお願いします。
54
+ よろしくお願いします。
55
+
56
+ 教えていただいた情報を読みコードを書いてみました。
57
+
58
+ ```vb.net
59
+ Using cn As NpgsqlConnection = New NpgsqlConnection()
60
+ cn.ConnectionString = 接続文字列
61
+ cn.Open()
62
+ Dim strSelectSQL As String = セレクト文
63
+ Dim nSelectCmd As NpgsqlCommand = New NpgsqlCommand(strSelectSQL, cn)
64
+ Dim dr As NpgsqlDataReader = nSelectCmd.ExecuteReader
65
+ If (dr.HasRows = False) Then
66
+ 'Dim trn As NpgsqlTransaction = cn.BeginTransaction()
67
+ Try
68
+ Dim strInsertSQL As String = インサート文
69
+ Dim nInsertCmd As NpgsqlCommand = New NpgsqlCommand(strInsertSQL, cn)
70
+ Dim intCount As Int32 = nInsertCmd.ExecuteNonQuery()
71
+ 'trn.Commit()
72
+ nInsertCmd.Dispose()
73
+ Catch ex As Exception
74
+ 'trn.Rollback()
75
+ Dim strDateTime As String = Format(Now, "yyyy-MM-dd hh:mm:ss")
76
+ Call sWriteErrorLog(strDateTime, "DB選択", ex.ToString)
77
+ Finally
78
+ dr.Dispose()
79
+ nSelectCmd.Dispose()
80
+ End Try
81
+ End If
82
+ End Using
83
+ ```
84
+
85
+ コメントアウトしていますが、Dim trn As NpgsqlTransaction = cn.BeginTransaction()でエラーになります。
86
+
87
+ またDim intCount As Int32 = nInsertCmd.ExecuteNonQuery()でcatchに行き以下のようなエラーが出ます。
88
+
89
+ 「"A command is already in progress: セレクトのSQL文"」
90
+
91
+ vb.netが初心者に近いので解らないのですが。
92
+
93
+ 何が良くないのでしょうか?
94
+ なぜトランザクションがエラーになるのでしょうか?