回答編集履歴

1

重複投稿にて削除しました

2017/09/05 04:21

投稿

dotnet
dotnet

スコア25

test CHANGED
@@ -1,159 +1 @@
1
- 自分がfunctionの仕様を理解しきれていなかったことによる自爆で(language→sql、plpgsql)、Oracleと同じように書いたらすんなり期待通りの結果が得られました。長くなり恐縮ですが、省略せずに試したことを記載します。
1
+           
2
-
3
-
4
-
5
- 以下、自己検証です。全言語において独習者ですので「おいおい」といった箇所があれば是非教えてください。
6
-
7
-
8
-
9
- ```
10
-
11
- --テスト用テーブル
12
-
13
- create table test_table(
14
-
15
- userid varchar(20),
16
-
17
- password varchar(20),
18
-
19
- usertext varchar(20),
20
-
21
- status varchar(20),
22
-
23
- constraint pk_test_table primary key(userid));
24
-
25
-
26
-
27
- --ダミーデータ
28
-
29
- insert into test_table(userid,password,usertext,status)
30
-
31
- values('hoge','fuga','hogeさん','active');
32
-
33
-
34
-
35
- --ストアド
36
-
37
- create or replace function testFunction(in p_userid varchar,in p_password varchar,out v_usertext varchar,out v_status varchar) as $$
38
-
39
- begin
40
-
41
- select usertext,status into v_usertext,v_status from test_table where userid=p_userid and password=p_password;
42
-
43
- end; $$ language plpgsql;
44
-
45
-
46
-
47
- //C#側 ユーザー名:textBox1.text パスワード:textBox2.text 処理開始:button1
48
-
49
- private void button1_Click(object sender, EventArgs e)
50
-
51
- {
52
-
53
- using (NpgsqlConnection con = new NpgsqlConnection("接続文字列"))
54
-
55
- {
56
-
57
- using (NpgsqlCommand cmd = new NpgsqlCommand("testFunction",con))
58
-
59
- {
60
-
61
- cmd.CommandType = CommandType.StoredProcedure;
62
-
63
-
64
-
65
- NpgsqlParameter p_userid =
66
-
67
- cmd.Parameters.Add("p_userid", NpgsqlDbType.Varchar);
68
-
69
- p_userid.Direction = ParameterDirection.Input;
70
-
71
- p_userid.Value = this.textBox1.Text;
72
-
73
-
74
-
75
- NpgsqlParameter p_passwod =
76
-
77
- cmd.Parameters.Add("p_password", NpgsqlDbType.Varchar);
78
-
79
- p_passwod.Direction = ParameterDirection.Input;
80
-
81
- p_passwod.Value = this.textBox2.Text;
82
-
83
-
84
-
85
- NpgsqlParameter v_usertext =
86
-
87
- cmd.Parameters.Add("v_usertext", NpgsqlDbType.Integer);
88
-
89
- v_usertext.Direction = ParameterDirection.Output;
90
-
91
-
92
-
93
- NpgsqlParameter v_status =
94
-
95
- cmd.Parameters.Add("v_status", NpgsqlDbType.Varchar);
96
-
97
- v_status.Direction = ParameterDirection.Output;
98
-
99
-
100
-
101
- try
102
-
103
- {
104
-
105
- con.Open();
106
-
107
- cmd.ExecuteNonQuery();
108
-
109
- }
110
-
111
- catch(NpgsqlException ex)
112
-
113
- {
114
-
115
- MessageBox.Show(ex.Message.ToString());
116
-
117
- return;
118
-
119
- }
120
-
121
- MessageBox.Show(
122
-
123
- "ユーザー名:" + v_usertext.Value.ToString() + "\r\n" +
124
-
125
- "ステータス:" + v_status.Value.ToString());
126
-
127
- }
128
-
129
- }
130
-
131
- }
132
-
133
-
134
-
135
- 引数 textBox1.text hoge, textBox2.text fuga
136
-
137
-
138
-
139
- 実行結果
140
-
141
- ユーザー名:hogeさん
142
-
143
- ステータス:active
144
-
145
-
146
-
147
- ```
148
-
149
-
150
-
151
- ベストアンサーについては
152
-
153
- (1)ベストアンサー選択=解決済みであることを知らずにクリックしてしまった
154
-
155
- (2)当初の疑問が解決されたという認識はありませんでしたが、language 'sql' の場合はこう使えばよかったのか!への謝意
156
-
157
-
158
-
159
- このようにご理解いただければ幸いです。ご回答・コメントくださった皆々さま、本当にありがとうございました。