MySQL接続テスト(C#コンソール)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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