win7で、vb2017(community版)、postgresql9.6を使っています。
TESTテーブルをselectしてデータがあればupdate、なければinsertしようと思っています。
何とかソースを書いたんですが、うまく動作しません。
Npgsqlを使っています。
vb.net
1 cnは接続文字列です。 2 Dim strSQL As String = "SELECT * FROM public.TEST WHERE ID='" & Code.ToString() & "';" 3 Dim cmd As NpgsqlCommand = New NpgsqlCommand(strSQL, cn) 4 Dim reader As NpgsqlDataReader = cmd.ExecuteReader() 5 If reader.HasRows = False Then 6 インサート文 7 Else 8 アップデート文 9 End If
dim readerのところでエラーになります。
「Npgsql.NpgsqlException: '外部コンポーネントが例外をスローしました。'」
コード自体間違っているのでしょうか。
ソースをまとめてみました。
vb.net
1 Dim npgCnct As NpgsqlConnection 2 Dim npgCmnd As NpgsqlCommand 3 Dim npgDR As NpgsqlDataReader 4 Dim strSql As String 5 6 npgCnct = New Npgsql.NpgsqlConnection 7 npgCnct.ConnectionString = "Server=localhost;Port=5432;User Id=user1;Password=pass;Database=TESTDB;" 8 npgCnct.Open() 9 10 strSql = "SELECT * FROM public.TEST;" 11 12 npgCmnd = New NpgsqlCommand 13 npgCmnd.Connection = npgCnct 14 npgCmnd.CommandText = strSql 15 npgDR = npgCmnd.ExecuteReader()
やはりnpgDR = npgCmnd.ExecuteReader()のところで下記のエラーが出ます。
「Npgsql.PostgresException: '外部コンポーネントが例外をスローしました。'」
なぜエラーになるのでしょうか?
回答2件
あなたの回答
tips
プレビュー