[C#]使用MYSQL数据库

方法一:使用 MySQLDriverCS-n-EasyQueryTools
下载地址 http://sourceforge.net/project/showfiles.php?group_id=63165&package_id=60849
(http://downloads.sourceforge.net/mysqldrivercs/MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe?modtime=1184281287&big_mirror=0)

using MySQLDriverCS;


namespace DAL
{
    public class sqlHelper
    {
        public static DataTable GetDataTableBySql(string sql)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["conLogServer"].ToString();
            MySQLCommand cmd = new MySQLCommand();
            DataTable dt = new DataTable();

            MySQLConnection conn =  new MySQLConnection(new MySQLConnectionString("10.73.8.243","newdb","client","123").AsString);//connectionString))
           
            {
                MySQLDataAdapter SqlDA = new MySQLDataAdapter();
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    SqlDA.SelectCommand = cmd;
                    SqlDA.Fill(dt);
                    conn.Close();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    cmd.Dispose();
                    cmd = null;
                    SqlDA.Dispose();
                    SqlDA = null;
                }
                return dt;
            }
        }

方法二:使用 mysql-connector-net-5.0.6-noinstall.zip
下载地址 http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-5.0.8.1-noinstall.zip/from/pick#mirrors

using MySql.Data.MySqlClient;


namespace DAL
{
    public class sqlHelper
    {
        public static DataTable GetDataTableBySql(string sql)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["conLogServer"].ToString();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt = new DataTable();

            MySqlConnection conn =  new MySqlConnection(new MySqlConnectionString("10.73.8.243","newdb","client","123").AsString);//connectionString))
            conn.Open();
            {
                MySqlDataAdapter SqlDA = new MySqlDataAdapter();
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    SqlDA.SelectCommand = cmd;
                    SqlDA.Fill(dt);
                    conn.Close();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    cmd.Dispose();
                    cmd = null;
                    SqlDA.Dispose();
                    SqlDA = null;
                }
                return dt;
            }
        }


你可能感兴趣的:(mysql)