[Win] ADO.net MySQL ODBC Example with C#

博客首页 » Win ADO.net MySQL ODBC Example with C#

发布于 28 Feb 2015 14:41
标签 blog
MSDN ADO.net Example in C# doesn't provide MySQL ODBC example. I wrote one as a memo.

using System;
using System.Data;
using System.Data.Odbc;
 
namespace ConsoleApplication1
{
    /// <summary>
    /// Class1 
    /// </summary>
    class Class1
    {
        /// <summary>
        /// Application Entry Point
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Your Code
            //
            string connectionString = GetConnectionString();
            string queryString =
                "SELECT * FROM tt";
            using (OdbcConnection connection =
                       new OdbcConnection(connectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = queryString;
 
                try
                {
                    connection.Open();
 
                    OdbcDataReader reader = command.ExecuteReader();
 
                    while (reader.Read())
                    {
                        Console.WriteLine("\t{0}\t{1}",
                            reader[0], reader[1]);
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.WriteLine("Press ENTER to continue...");
            String line = Console.ReadLine();
        }
 
        static private string GetConnectionString()
        {
            // To avoid storing the connection string in your code, 
            // you can retrieve it from a configuration file.
            // Assumes Northwind.mdb is located in the c:\Data folder.
            return 
                "Driver={MySQL ODBC 5.3 Unicode Driver};" 
                + "Database=" + "demo" + ";" 
                + "User=" + "user" + ";"  
                + "Password=" + "" + ";"  
                + "Server=" + "localhost" + ";"  
                + "Port=" + "3306" + ";"  
                + "Option=3;";
                //"Driver={Microsoft Access Driver (*.mdb)};"
                //+ "Dbq=c:\\Data\\Northwind.mdb;Uid=Admin;Pwd=;";
        }    
    }
}

Reference
https://msdn.microsoft.com/en-us/library/dw70f090(v=vs.80).aspx

Provider Independent
https://msdn.microsoft.com/en-us/library/t9f29wbk(v=vs.80).aspx


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Win ADO.net MySQL ODBC Example with C#

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment