Skip to content

Instantly share code, notes, and snippets.

@mrgarita
Last active February 5, 2021 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgarita/6dd172fee8f9c10639a83e506f78bb7a to your computer and use it in GitHub Desktop.
Save mrgarita/6dd172fee8f9c10639a83e506f78bb7a to your computer and use it in GitHub Desktop.
MySQL接続テスト(C#コンソール)
/*
Visual Studio 2019からMySQLに接続するプログラムを作成する前の準備事項
ファイルメニューから プロジェクト > NuGet パッケージの管理(N)... を選択
参照タブを選択し「mysql」で検索して表示される「MySql.Data」をインストールする
*/
using System;
// MySQLを使うため
using MySql.Data.MySqlClient;
namespace MySQL接続テスト
{
class Program
{
static void Main(string[] args)
{
// MySQLへの接続情報
string server = "localhost";
string database = "mysql";
string user = "root";
string pass = "";
string charset = "utf8";
string connectionString = string.Format("Server={0};Database={1};Uid={2};Pwd={3};Charset={4}", server, database, user, pass, charset);
// MySQLへの接続
try
{
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open(); // 接続
Console.WriteLine("MySQLに接続しました!");
connection.Close(); // 接続の解除
}
catch (MySqlException me)
{
Console.WriteLine("ERROR: " + me.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment