@@ -7,7 +7,7 @@ ms.service: postgresql
7
7
ms.custom : mvc, devcenter
8
8
ms.devlang : csharp
9
9
ms.topic : quickstart
10
- ms.date : 02/28/2018
10
+ ms.date : 03/12/2019
11
11
---
12
12
13
13
# Azure Database for PostgreSQL: Use .NET (C#) to connect and query data
@@ -39,10 +39,6 @@ Replace the Host, DBName, User, and Password parameters with the values that you
39
39
40
40
``` csharp
41
41
using System ;
42
- using System .Collections .Generic ;
43
- using System .Linq ;
44
- using System .Text ;
45
- using System .Threading .Tasks ;
46
42
using Npgsql ;
47
43
48
44
namespace Driver
@@ -63,44 +59,46 @@ namespace Driver
63
59
//
64
60
string connString =
65
61
String .Format (
66
- " Server={0}; User Id ={1}; Database={2}; Port={3}; Password={4}; SSL Mode =Prefer; Trust Server Certificate=true " ,
62
+ " Server={0};Username ={1};Database={2};Port={3};Password={4};SSLMode =Prefer" ,
67
63
Host ,
68
64
User ,
69
65
DBname ,
70
66
Port ,
71
67
Password );
72
68
73
- var conn = new NpgsqlConnection (connString );
74
69
75
- Console .Out .WriteLine (" Opening connection" );
76
- conn .Open ();
70
+ using (var conn = new NpgsqlConnection (connString ))
77
71
78
- var command = conn .CreateCommand ();
79
- command .CommandText = " DROP TABLE IF EXISTS inventory;" ;
80
- command .ExecuteNonQuery ();
81
- Console .Out .WriteLine (" Finished dropping table (if existed)" );
82
-
83
- command .CommandText = " CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);" ;
84
- command .ExecuteNonQuery ();
85
- Console .Out .WriteLine (" Finished creating table" );
86
-
87
- command .CommandText =
88
- String .Format (
89
- @"
90
- INSERT INTO inventory (name, quantity) VALUES ({0}, {1});
91
- INSERT INTO inventory (name, quantity) VALUES ({2}, {3});
92
- INSERT INTO inventory (name, quantity) VALUES ({4}, {5});
93
- " ,
94
- " \' banana\' " , 150 ,
95
- " \' orange\' " , 154 ,
96
- " \' apple\' " , 100
97
- );
98
-
99
- int nRows = command .ExecuteNonQuery ();
100
- Console .Out .WriteLine (String .Format (" Number of rows inserted={0}" , nRows ));
101
-
102
- Console .Out .WriteLine (" Closing connection" );
103
- conn .Close ();
72
+ {
73
+ Console .Out .WriteLine (" Opening connection" );
74
+ conn .Open ();
75
+
76
+ using (var command = new NpgsqlCommand (" DROP TABLE IF EXISTS inventory" , conn ))
77
+ {
78
+ command .ExecuteNonQuery ();
79
+ Console .Out .WriteLine (" Finished dropping table (if existed)" );
80
+
81
+ }
82
+
83
+ using (var command = new NpgsqlCommand (" CREATE TABLE inventory(id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER)" , conn ))
84
+ {
85
+ command .ExecuteNonQuery ();
86
+ Console .Out .WriteLine (" Finished creating table" );
87
+ }
88
+
89
+ using (var command = new NpgsqlCommand (" INSERT INTO inventory (name, quantity) VALUES (@n1, @q1), (@n2, @q2), (@n3, @q3)" , conn ))
90
+ {
91
+ command .Parameters .AddWithValue (" n1" , " banana" );
92
+ command .Parameters .AddWithValue (" q1" , 150 );
93
+ command .Parameters .AddWithValue (" n2" , " orange" );
94
+ command .Parameters .AddWithValue (" q2" , 154 );
95
+ command .Parameters .AddWithValue (" n3" , " apple" );
96
+ command .Parameters .AddWithValue (" q3" , 100 );
97
+
98
+ int nRows = command .ExecuteNonQuery ();
99
+ Console .Out .WriteLine (String .Format (" Number of rows inserted={0}" , nRows ));
100
+ }
101
+ }
104
102
105
103
Console .WriteLine (" Press RETURN to exit" );
106
104
Console .ReadLine ();
@@ -116,10 +114,6 @@ Replace the Host, DBName, User, and Password parameters with the values that you
116
114
117
115
``` csharp
118
116
using System ;
119
- using System .Collections .Generic ;
120
- using System .Linq ;
121
- using System .Text ;
122
- using System .Threading .Tasks ;
123
117
using Npgsql ;
124
118
125
119
namespace Driver
@@ -140,36 +134,37 @@ namespace Driver
140
134
//
141
135
string connString =
142
136
String .Format (
143
- " Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};" ,
137
+ " Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer " ,
144
138
Host ,
145
139
User ,
146
140
DBname ,
147
141
Port ,
148
142
Password );
149
143
150
- var conn = new NpgsqlConnection (connString );
151
-
152
- Console .Out .WriteLine (" Opening connection" );
153
- conn .Open ();
154
-
155
- var command = conn .CreateCommand ();
156
- command .CommandText = " SELECT * FROM inventory;" ;
157
-
158
- var reader = command .ExecuteReader ();
159
- while (reader .Read ())
144
+ using (var conn = new NpgsqlConnection (connString ))
160
145
{
161
- Console .WriteLine (
162
- string .Format (
163
- " Reading from table=({0}, {1}, {2})" ,
164
- reader .GetInt32 (0 ).ToString (),
165
- reader .GetString (1 ),
166
- reader .GetInt32 (2 ).ToString ()
167
- )
168
- );
169
- }
170
146
171
- Console .Out .WriteLine (" Closing connection" );
172
- conn .Close ();
147
+ Console .Out .WriteLine (" Opening connection" );
148
+ conn .Open ();
149
+
150
+
151
+ using (var command = new NpgsqlCommand (" SELECT * FROM inventory" , conn ))
152
+ {
153
+
154
+ var reader = command .ExecuteReader ();
155
+ while (reader .Read ())
156
+ {
157
+ Console .WriteLine (
158
+ string .Format (
159
+ " Reading from table=({0}, {1}, {2})" ,
160
+ reader .GetInt32 (0 ).ToString (),
161
+ reader .GetString (1 ),
162
+ reader .GetInt32 (2 ).ToString ()
163
+ )
164
+ );
165
+ }
166
+ }
167
+ }
173
168
174
169
Console .WriteLine (" Press RETURN to exit" );
175
170
Console .ReadLine ();
@@ -186,10 +181,6 @@ Replace the Host, DBName, User, and Password parameters with the values that you
186
181
187
182
``` csharp
188
183
using System ;
189
- using System .Collections .Generic ;
190
- using System .Linq ;
191
- using System .Text ;
192
- using System .Threading .Tasks ;
193
184
using Npgsql ;
194
185
195
186
namespace Driver
@@ -210,36 +201,36 @@ namespace Driver
210
201
//
211
202
string connString =
212
203
String .Format (
213
- " Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};" ,
204
+ " Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer " ,
214
205
Host ,
215
206
User ,
216
207
DBname ,
217
208
Port ,
218
209
Password );
219
210
220
- var conn = new NpgsqlConnection (connString );
221
-
222
- Console .Out .WriteLine (" Opening connection" );
223
- conn .Open ();
224
-
225
- var command = conn .CreateCommand ();
226
- command .CommandText =
227
- String .Format (" UPDATE inventory SET quantity = {0} WHERE name = {1};" ,
228
- 200 ,
229
- " \' banana\' "
230
- );
231
-
232
- int nRows = command .ExecuteNonQuery ();
233
- Console .Out .WriteLine (String .Format (" Number of rows updated={0}" , nRows ));
211
+ using (var conn = new NpgsqlConnection (connString ))
212
+ {
234
213
235
- Console .Out .WriteLine (" Closing connection" );
236
- conn .Close ();
214
+ Console .Out .WriteLine (" Opening connection" );
215
+ conn .Open ();
216
+
217
+ using (var command = new NpgsqlCommand (" UPDATE inventory SET quantity = @q WHERE name = @n" , conn ))
218
+ {
219
+ command .Parameters .AddWithValue (" n" , " banana" );
220
+ command .Parameters .AddWithValue (" q" , 200 );
221
+
222
+ int nRows = command .ExecuteNonQuery ();
223
+ Console .Out .WriteLine (String .Format (" Number of rows updated={0}" , nRows ));
224
+ }
225
+ }
237
226
238
227
Console .WriteLine (" Press RETURN to exit" );
239
228
Console .ReadLine ();
240
229
}
241
230
}
242
231
}
232
+
233
+
243
234
```
244
235
245
236
@@ -252,10 +243,6 @@ Replace the Host, DBName, User, and Password parameters with the values that you
252
243
253
244
``` csharp
254
245
using System ;
255
- using System .Collections .Generic ;
256
- using System .Linq ;
257
- using System .Text ;
258
- using System .Threading .Tasks ;
259
246
using Npgsql ;
260
247
261
248
namespace Driver
@@ -276,33 +263,33 @@ namespace Driver
276
263
//
277
264
string connString =
278
265
String .Format (
279
- " Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};" ,
266
+ " Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer " ,
280
267
Host ,
281
268
User ,
282
269
DBname ,
283
270
Port ,
284
271
Password );
285
272
286
- var conn = new NpgsqlConnection (connString );
287
-
288
- Console .Out .WriteLine (" Opening connection" );
289
- conn .Open ();
273
+ using ( var conn = new NpgsqlConnection (connString ))
274
+ {
275
+ Console .Out .WriteLine (" Opening connection" );
276
+ conn .Open ();
290
277
291
- var command = conn .CreateCommand ();
292
- command .CommandText =
293
- String .Format (" DELETE FROM inventory WHERE name = {0};" ,
294
- " \' orange\' " );
295
- int nRows = command .ExecuteNonQuery ();
296
- Console .Out .WriteLine (String .Format (" Number of rows deleted={0}" , nRows ));
278
+ using (var command = new NpgsqlCommand (" DELETE FROM inventory WHERE name = @n" , conn ))
279
+ {
280
+ command .Parameters .AddWithValue (" n" , " orange" );
297
281
298
- Console .Out .WriteLine (" Closing connection" );
299
- conn .Close ();
282
+ int nRows = command .ExecuteNonQuery ();
283
+ Console .Out .WriteLine (String .Format (" Number of rows deleted={0}" , nRows ));
284
+ }
285
+ }
300
286
301
287
Console .WriteLine (" Press RETURN to exit" );
302
288
Console .ReadLine ();
303
289
}
304
290
}
305
291
}
292
+
306
293
```
307
294
308
295
## Next steps
0 commit comments