You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This quickstart demonstrates how to connect to an Azure Database for PostgreSQL using a C# application. It shows how to use SQL statements to query, insert, update, and delete data in the database. The steps in this article assume that you are familiar with developing using C#, and that you are new to working with Azure Database for PostgreSQL.
20
20
21
21
## Prerequisites
22
+
22
23
For this quickstart you need:
23
24
24
25
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
@@ -29,6 +30,7 @@ For this quickstart you need:
29
30
- Install [Npgsql](https://www.nuget.org/packages/Npgsql/) NuGet package in Visual Studio.
30
31
31
32
## Get connection information
33
+
32
34
Get the connection information needed to connect to the Azure Database for PostgreSQL. You need the fully qualified server name and login credentials.
33
35
34
36
1. Log in to the [Azure portal](https://portal.azure.com/).
@@ -38,6 +40,7 @@ Get the connection information needed to connect to the Azure Database for Postg
38
40
:::image type="content" source="./media/connect-csharp/1-connection-string.png" alt-text="Azure Database for PostgreSQL server name":::
39
41
40
42
## Step 1: Connect and insert data
43
+
41
44
Use the following code to connect and load the data using **CREATE TABLE** and **INSERT INTO** SQL statements. The code uses NpgsqlCommand class with method:
42
45
-[Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to the PostgreSQL database.
43
46
-[CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) sets the CommandText property.
@@ -119,6 +122,7 @@ namespace Driver
119
122
[Having any issues? Let us know.](https://github.com/MicrosoftDocs/azure-docs/issues)
120
123
121
124
## Step 2: Read data
125
+
122
126
Use the following code to connect and read the data using a **SELECT** SQL statement. The code uses NpgsqlCommand class with method:
123
127
-[Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to PostgreSQL.
124
128
-[CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) and [ExecuteReader()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlCommand.html#Npgsql_NpgsqlCommand_ExecuteReader) to run the database commands.
@@ -193,6 +197,7 @@ namespace Driver
193
197
194
198
195
199
## Step 3: Update data
200
+
196
201
Use the following code to connect and update the data using an **UPDATE** SQL statement. The code uses NpgsqlCommand class with method:
197
202
-[Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to PostgreSQL.
198
203
-[CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand), sets the CommandText property.
@@ -257,6 +262,7 @@ namespace Driver
257
262
[Having any issues? Let us know.](https://github.com/MicrosoftDocs/azure-docs/issues)
258
263
259
264
## Step 4: Delete data
265
+
260
266
Use the following code to connect and delete data using a **DELETE** SQL statement.
261
267
262
268
The code uses NpgsqlCommand class with method [Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to the PostgreSQL database. Then, the code uses the [CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) method, sets the CommandText property, and calls the method [ExecuteNonQuery()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlCommand.html#Npgsql_NpgsqlCommand_ExecuteNonQuery) to run the database commands.
@@ -326,6 +332,7 @@ az group delete \
326
332
```
327
333
328
334
## Next steps
335
+
329
336
> [!div class="nextstepaction"]
330
337
> [Manage Azure Database for MySQL server using Portal](./how-to-manage-server-portal.md)<br/>
Copy file name to clipboardExpand all lines: articles/postgresql/single-server/connect-csharp.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,7 @@ ms.date: 10/18/2020
19
19
This quickstart demonstrates how to connect to an Azure Database for PostgreSQL using a C# application. It shows how to use SQL statements to query, insert, update, and delete data in the database. The steps in this article assume that you are familiar with developing using C#, and that you are new to working with Azure Database for PostgreSQL.
20
20
21
21
## Prerequisites
22
+
22
23
For this quickstart you need:
23
24
24
25
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
@@ -36,6 +37,7 @@ For this quickstart you need:
36
37
- Install [Npgsql](https://www.nuget.org/packages/Npgsql/) NuGet package in Visual Studio.
37
38
38
39
## Get connection information
40
+
39
41
Get the connection information needed to connect to the Azure Database for PostgreSQL. You need the fully qualified server name and login credentials.
40
42
41
43
1. Log in to the [Azure portal](https://portal.azure.com/).
@@ -45,6 +47,7 @@ Get the connection information needed to connect to the Azure Database for Postg
45
47
:::image type="content" source="./media/connect-csharp/1-connection-string.png" alt-text="Azure Database for PostgreSQL server name":::
46
48
47
49
## Step 1: Connect and insert data
50
+
48
51
Use the following code to connect and load the data using **CREATE TABLE** and **INSERT INTO** SQL statements. The code uses NpgsqlCommand class with method:
49
52
-[Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to the PostgreSQL database.
50
53
-[CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) sets the CommandText property.
@@ -124,6 +127,7 @@ namespace Driver
124
127
```
125
128
126
129
## Step 2: Read data
130
+
127
131
Use the following code to connect and read the data using a **SELECT** SQL statement. The code uses NpgsqlCommand class with method:
128
132
-[Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to PostgreSQL.
129
133
-[CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) and [ExecuteReader()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlCommand.html#Npgsql_NpgsqlCommand_ExecuteReader) to run the database commands.
@@ -196,6 +200,7 @@ namespace Driver
196
200
```
197
201
198
202
## Step 3: Update data
203
+
199
204
Use the following code to connect and update the data using an **UPDATE** SQL statement. The code uses NpgsqlCommand class with method:
200
205
-[Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to PostgreSQL.
201
206
-[CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand), sets the CommandText property.
@@ -258,6 +263,7 @@ namespace Driver
258
263
```
259
264
260
265
## Step 4: Delete data
266
+
261
267
Use the following code to connect and delete data using a **DELETE** SQL statement.
262
268
263
269
The code uses NpgsqlCommand class with method [Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to the PostgreSQL database. Then, the code uses the [CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) method, sets the CommandText property, and calls the method [ExecuteNonQuery()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlCommand.html#Npgsql_NpgsqlCommand_ExecuteNonQuery) to run the database commands.
@@ -327,6 +333,7 @@ az group delete \
327
333
```
328
334
329
335
## Next steps
336
+
330
337
> [!div class="nextstepaction"]
331
338
> [Manage Azure Database for MySQL server using Portal](./how-to-create-manage-server-portal.md)<br/>
Copy file name to clipboardExpand all lines: articles/postgresql/single-server/connect-python.md
+7-34Lines changed: 7 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ In this quickstart, you will learn how to connect to the database on Azure Datab
23
23
24
24
25
25
## Prerequisites
26
+
26
27
For this quickstart you need:
27
28
28
29
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
@@ -41,6 +42,7 @@ For this quickstart you need:
41
42
- Install [psycopg2](https://pypi.python.org/pypi/psycopg2-binary/) using `pip install psycopg2-binary` in a terminal or command prompt window. For more information, see [how to install `psycopg2`](https://www.psycopg.org/docs/install.html).
42
43
43
44
## Get database connection information
45
+
44
46
Connecting to an Azure Database for PostgreSQL database requires the fully qualified server name and login credentials. You can get this information from the Azure portal.
45
47
46
48
1. In the [Azure portal](https://portal.azure.com/), search for and select your Azure Database for PostgreSQL server name.
@@ -57,6 +59,7 @@ Connecting to an Azure Database for PostgreSQL database requires the fully quali
57
59
> -`<database-name>` a default database named *postgres* was automatically created when you created your server. You can rename that database or [create a new database](https://www.postgresql.org/docs/current/sql-createdatabase.html) by using SQL commands.
58
60
59
61
## Step 1: Connect and insert data
62
+
60
63
The following code example connects to your Azure Database for PostgreSQL database using
61
64
-[psycopg2.connect](https://www.psycopg.org/docs/connection.html) function, and loads data with a SQL **INSERT** statement.
62
65
-[cursor.execute](https://www.psycopg.org/docs/cursor.html#execute) function executes the SQL query against the database.
cursor.execute("UPDATE inventory SET quantity = %s WHERE name = %s;", (200, "banana"))
167
143
print("Updated 1 row of data")
168
-
169
144
```
170
145
171
146
## Step 5: Delete data
147
+
172
148
The following code example runs [cursor.execute](https://www.psycopg.org/docs/cursor.html#execute) with the SQL **DELETE** statement to delete an inventory item that you previously inserted.
0 commit comments