MySql.Data.dll(VC#调用Mysql)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

/*
    mysql.data.dll是mysql数据库的.NET驱动,通常在mysql安装目录下的connector子目录下,如果未安装connector则需要下载安装 http://dev.mysql.com/downloads/connector/net/
    右击项目引用 -- 添加引用 -- 浏览找到 mysql.data.dll
    using MySql.Data.MySqlClient;
    string strConnection = "data source=服务器名称(such as 127.0.0.1);port=端口;database=数据库名称;user id=用户名称;password=密码;pooling=是否使用连接池;charset=字符集";
    ExecuteNonQuery insert/update/delete 返回1成功
    ExecuteReader select 结果多行多列,返回DataReader
    ExecuteScalar select 结果首行首列 
*/

namespace 操作mysql
{
    class Program
    {
        static MySqlConnection ConnectSql(string strConnection)
        {
            try
            {
                return new MySqlConnection(strConnection);
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }
        static MySqlCommand GetCmd(string strSql, MySqlConnection mysqlConnection)
        {
            try
            {
                return new MySqlCommand(strSql, mysqlConnection);
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }
        static bool DoExecuteNonQuery(MySqlConnection conn, MySqlCommand cmd)
        {
            try
            {
                conn.Open();
                int iRet = cmd.ExecuteNonQuery();
                conn.Close();
                if (iRet == 1)
                {
                    return true;
                }
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
            }
            return false;
        }
        static object DoExecuteScalar(MySqlConnection conn, MySqlCommand cmd)
        {
            try
            {
                conn.Open();
                object o = cmd.ExecuteScalar();
                conn.Close();
                if (o != null)
                {
                    return o;
                }
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }
        static int DoExecuteReader(MySqlConnection conn, MySqlCommand cmd)
        {
            int iCount = 0;
            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    iCount++;
                    dynamic col0 = " ";
                    if (!reader.IsDBNull(0))
                    {
                        col0 = reader.GetInt32(0);
                    }
                    dynamic col1 = " ";
                    if (!reader.IsDBNull(1))
                    {
                        col1 = reader.GetString(1);
                    }
                    dynamic col2 = " ";
                    if (!reader.IsDBNull(2))
                    {
                        col2 = reader.GetString(2);
                    }
                    dynamic col3 = " ";
                    if (!reader.IsDBNull(3))
                    {
                        col3 = reader.GetInt32(3);
                    }
                    dynamic col4 = " ";
                    if (!reader.IsDBNull(4))
                    {
                        col4 = reader.GetValue(4);
                    }
                    Console.WriteLine("{0},{1},{2},{3},{4}", col0, col1, col2, col3, col4);
                }
                conn.Close();
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
            }
            return iCount;
        }
        static void Main(string[] args)
        {
            #region 
            /*
                create table if not exists t_test(
                   id int unsigned auto_increment,
                   name varchar(22) not null,
                   password varchar(22) not null,
                   age int,
                   salary float,
                   primary key (id)
                )engine=innodb default charset=utf8;

                insert into t_test 
                ( name, password, age, salary ) 
                values 
                ( "a", "a", 11, 1234.5 );

                insert into t_test
                ( name, password, age, salary ) 
                values
                ( "b", "b", 11, 1234.5 ),
                ( "c", "c", 11, 1234.5 ),
                ( "d", "d", 11, 1234.5 );

                update t_test set name="aa", password="aa" where name="a";
                
                select name,salary from t_test;

                select * from t_test;
             */
            string strConnection = "data source=localhost;port=3306;database=db_test;user id=root;password=mysql;pooling=true;charset=utf8";
            MySqlConnection conn = ConnectSql(strConnection);
            if (conn != null)
            {
                //string strSql = "create table if not exists t_test(" +
                //                "id int unsigned auto_increment," +
                //                "name varchar(22) not null," +
                //                "password varchar(22) not null," +
                //                "age int," +
                //                "salary float," +
                //                "primary key(id)" +
                //                ")engine = innodb default charset= utf8;";

                //string strSql = "insert into t_test " +
                //                "( name, password, age, salary ) " +
                //                "values " +
                //                "( \"a\", \"a\", 11, 1234.5 );";

                //string strSql = "update t_test set name=\"aa\", password=\"aa\" where name=\"a\";";

                //string strSql = "select password from t_test where name=\"aa\";";

                string strSql = "select * from t_test";

                MySqlCommand cmd = GetCmd(strSql, conn);
                if (cmd != null)
                {
                    //if (DoExecuteNonQuery(conn, cmd))
                    //{
                    //    Console.WriteLine("success");
                    //}
                    //else
                    //{
                    //    Console.WriteLine("fail");
                    //}

                    //Console.WriteLine(DoExecuteScalar(conn, cmd));
                    Console.WriteLine(DoExecuteReader(conn, cmd));
                }
            }
            #endregion
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/dailycode/p/9639924.html