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

概要

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

クライアント API

Azure Database for MySQL にアクセスするための推奨されるクライアント ライブラリは、MySQL の Connector/Net です。 パッケージを使ってデータベースに接続し、SQL ステートメントを直接実行します。

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

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

Install-Package MySql.Data

.NET Core CLI

dotnet add package MySql.Data

コード例

MySQL データベースに接続してクエリを実行します。

/* Include this "using" directive...
using MySql.Data.MySqlClient;
*/

string connectionString = "Server=[servername].mysql.database.azure.com; " +
    "Database=myDataBase; Uid=[userid]@[servername]; Pwd=myPassword;";

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

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

サンプル