0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VsCode + C# + .NETCore 3.1 + MySqlConnector から mariadb10.5 にアクセスしてみる

Last updated at Posted at 2020-07-05

目的

・VsCode + C# + .NETCore 3.1 + MySqlConnector から mariadb10.5 にアクセスしてみる
・Windows 10 と Ubuntu 18.04 でコードをコピペして確認

##サンプルを試してみる
・VSCode を立ちあげて適当なフォルダを選択する
・コンソールから作成したフォルダ以下で dotnet new console
~/code/my0001$ ls
Program.cs bin my0001.csproj obj

MySqlConnectorを追加する

※事前にvscode-nuget-package-managerが追加されてることを確認
表示メニュー
 -> コマンドパレット
  -> nuget
   -> Nuget package Manager:Add Pckage
    -> MySqlConnector を入力後 enter
     -> MySqlConnector を選択
      -> 1.0.0-beta.5 を選択

サンプルコード

using System;
using MySqlConnector;
using System.Data;

namespace my0001
{
    class Program
    {
        // 接続情報
        private static readonly string Server = "192.168.5.xxx";    // ホスト名
        private static readonly int Port = 3306;                    // ポート番号
        private static readonly string Database = "nation";         // データベース名
        private static readonly string Uid = "demo";                // ユーザ名
        private static readonly string Pwd = "passwd";              // パスワード

        // 接続文字列
        private static readonly string ConnectionString = $"Server={Server}; Port={Port}; Database={Database}; Uid={Uid}; Pwd={Pwd}";

        static void Main(string[] args)
        {
            using (MySqlConnection conn = new MySqlConnection(ConnectionString))
            {
                conn.Open();

                MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM guests", conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "guests");
                DataTable tbl = ds.Tables["guests"];

                foreach(DataRow row in tbl.Rows)
                {
                    Console.WriteLine("Column 0: {0} Column 1: {1}", row[0].ToString(), row[1].ToString());
                }
            }
        }
    }
}

参考にしたサイトはこちら

Ubuntu 18.04 + VsCode + Pytho3 + mysql-connector-python から mariadb10.5 にアクセスしてみる
Ubuntu 20.04 に Posgtgres12 をインストール後 C# + Npgsql でアクセスしてみる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?