.NET CORE 操作Mysql数据库例子

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012997311/article/details/80098191

首先要添加Data.Mysql (NuGet)

MYSQLHELPER.cs

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Data;
namespace dotNetMysql
{
     public  class MYSQLHELPER
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        public static string conf = "server=127.0.0.1;user id=root;password=root;database=world;sslmode=None";
        /// <summary>
        /// 返回首行首列
        /// </summary>
        /// <param name="sqltext"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sqltext)
        {
            using (MySqlConnection conn = new MySqlConnection(conf))
            {
                conn.Open();
                MySqlCommand comm = new MySqlCommand(sqltext, conn);
               return  comm.ExecuteScalar();
            }
        }
        /// <summary>
        /// 返回受影响行数
        /// </summary>
        /// <param name="sqltext"></param>
        /// <returns></returns>
        public static int ExecuteNoQuery(string sqltext)
        {
            using (MySqlConnection conn = new MySqlConnection(conf))
            {
                conn.Open();
                MySqlCommand comm = new MySqlCommand(sqltext, conn);
                return comm.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 返回受影响行数
        /// </summary>
        /// <param name="sqltext"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataset(string sqltext)
        {
            using (MySqlConnection conn = new MySqlConnection(conf))
            {
                conn.Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter(sqltext, conf);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds;
            }
        }
        /// <summary>
        /// 返回dataset 传入sqlparameter
        /// </summary>
        /// <param name="sqltext"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataset(string sqltext, MySqlParameter[] param)
        {
            using (MySqlConnection conn = new MySqlConnection(conf))
            {
                conn.Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter(sqltext,conn);
                //adapter.SelectCommand.Connection = conn;
                adapter.SelectCommand.CommandType = CommandType.Text;
              //  adapter.SelectCommand.CommandText = sqltext;
                adapter.SelectCommand.Parameters.AddRange(param);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds;
            }
        }
    }
}

Program.cs

using MySql.Data.MySqlClient;
using System;
using System.Data;
namespace dotNetMysql
{
    class Program
    {
        static void Main(string[] args)
        {
           // Console.WriteLine("Hello World!");
            string conf = "server=127.0.0.1;user id=root;password=root;database=world;sslmode=None";
            using (MySqlConnection conn = new MySqlConnection(conf))
            {
                
                conn.Open();
                MySqlDataAdapter comm = new MySqlDataAdapter("select * from `city` where `Name` like '%Shanghai%'", conn);
                DataSet ds = new DataSet();
                comm.Fill(ds);
                DataTable dt = ds.Tables[0];
                Console.WriteLine(dt.Rows.Count);
                Console.WriteLine(dt.Rows[0]["Name"].ToString());
            }
            //////////////////////////////////
            DataTable dt2 = MYSQLHELPER.ExecuteDataset("select * from `country` where `Name` like '%Chin%'").Tables[0];
            Console.WriteLine("国家名称:"+dt2.Rows[0]["Name"]);
            ////
            MySqlParameter[] param ={
                    new MySqlParameter("@China",MySqlDbType.VarChar,52)
            };
            param[0].Value = "China";
            DataTable dt3= MYSQLHELPER.ExecuteDataset("select * from `country` where `Name` = @China", param).Tables[0];//注意(这里的@China不用用''括起来)
            Console.WriteLine("地区:"+dt3.Rows[0]["Region"]);
            Console.ReadLine();
        }
    }
}

运行结果:

代码下载:代码下载

展开阅读全文

没有更多推荐了,返回首页