次の方法で共有


.NET 用 Azure Database for PostgreSQL ライブラリ

概要

Azure Database for PostgreSQL に格納されているデータとリソースを操作します。

クライアント API

Azure Database for PostgreSQL にアクセスするための推奨されるクライアント ライブラリは、オープンソースの Npgsql ADO.NET データ プロバイダーです。 この ADO.NET プロバイダーを使用してデータベースに接続し、SQL ステートメントを直接実行するか、Npgsql の Entity Framework 6 または Entity Framework Core プロバイダーを使用して、Entity Framework 経由で SQL ステートメントを実行します。

NuGet パッケージを Visual Studio パッケージ マネージャー コンソールから直接インストールするか、.NET Core CLI を使ってインストールします。

Visual Studio パッケージ マネージャー

Install-Package Npgsql

.NET Core CLI

dotnet add package Npgsql

コード例

/* Include this 'using' directive...
using Npgsql;
*/

// Always store connection strings securely. 
string connectionString = "Server=[servername].postgres.database.azure.com; " +
    "Port=5432; Database=myDataBase; User Id=[userid]@[servername]; Password=password;";

// Best practice is to scope the NpgsqlConnection to a "using" block
using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
    // Connect to the database
    conn.Open();

    // Read rows
    NpgsqlCommand selectCommand = new NpgsqlCommand("SELECT * FROM MyTable", conn);
    NpgsqlDataReader results = selectCommand.ExecuteReader();
    
    // Enumerate over the rows
    while(results.Read())
    {
        Console.WriteLine("Column 0: {0} Column 1: {1}", results[0], results[1]);
    }
}

サンプル