Archive for 4月 1st, 2013

  1. Hello, Connector/NET World!

    Posted on 4月 1st, 2013 by cx20

    Connector/NET

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は C# による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(C# + Connector/NET + MySQL)

    using System;
    using MySql.Data.MySqlClient;
     
    class Hello
    {
        static void Main( String[] args )
        {
            string conStr = "server=localhost;user id=root;password=P@ssW0rd";
            string sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
            MySqlConnection con = new MySqlConnection(conStr);
            MySqlCommand  cmd = new MySqlCommand(sqlStr, con);
            con.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            while( reader.Read() )
            {
                Console.WriteLine( reader.GetName(0) );
                Console.WriteLine( "---------------------" );
                Console.WriteLine( reader[0] );
            }
            reader.Close();
            con.Close();
        }
    }

    コンパイル方法

    C:¥> csc /r:MySql.Data.dll Hello.cs

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!