プロジェクトの作成
mkdir Read01
cd Read01
dotnet new console
dotnet add package MySql.Data --version 8.0.30
フォルダー構造
$ tree -L 1
.
├── Program.cs
├── Read01.csproj
└── obj
Program.cs
// ----------------------------------------------------------------
/*
Program.cs
Aug/09/2022
*/
// ----------------------------------------------------------------
using System;
using MySql.Data.MySqlClient;
// ----------------------------------------------------------------
public static class maria_read
{
public static void Main (string[] args)
{
string server = "localhost";
string str_db = "city";
string user = "scott";
string password = "tiger123";
string str_connect = "Server=" + server +
";User Id=" + user + ";Password=" + password + ";Database=" + str_db + ";";
MySqlConnection conn = new MySqlConnection (str_connect);
Console.Error.WriteLine ("*** 開始 ***");
conn.Open();
MySqlCommand command = new MySqlCommand("select * from cities", conn);
MySqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
for (int it = 0; it < dr.FieldCount; it++)
{
Console.Write("{0} \t", dr[it]);
}
Console.WriteLine();
}
conn.Close();
Console.Error.WriteLine ("*** 終了 ***");
}
// ----------------------------------------------------------------
}
// ----------------------------------------------------------------
実行結果
$ dotnet run
*** 開始 ***
t3321 岡山 497125 2009-8-4
t3322 倉敷 219687 2009-9-2
t3323 津山 871392 2009-10-8
t3324 玉野 923187 2009-5-9
t3325 笠岡 651978 2009-1-5
t3326 井原 395647 2009-5-22
t3327 総社 412786 2009-7-17
t3328 高梁 174835 2009-3-4
t3329 新見 781324 2009-10-12
*** 終了 ***