码迷,mamicode.com
首页 > 移动开发 > 详细

在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查

时间:2018-03-12 20:56:08      阅读:514      评论:0      收藏:0      [点我收藏+]

标签:data   rgs   分享图片   host   int   sum   summary   database   条件   

一、首先先创建一个C#的控制台应用

技术分享图片

 

二、然后添加上必要的命名空间

using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Data;
using Dapper;
using System.Linq;

 

三、首先配置好一个数据库中存在的类表:

class Info
    {
        public int action_id { get; set; }
        public int parent_id { get; set; }
        public string action_code { get; set; }
        public string relevance { get; set; }
    }

 

技术分享图片

 

四、添加Dapper和MYSQL的扩展包

添加Dapper扩展包

技术分享图片

添加Mysql扩展包
技术分享图片

 

最后在最右边的管理中可以查看到自己刚刚添加的扩展:

技术分享图片

 

五、添加数据库链接代码

//数据库连接配置信息;
public static string constr = "server = localhost;User Id = root;password = 123456;Database = ecshop";

  再添加无条件查询的代码:

        /// <summary>
        /// 无条件查询所有;
        /// </summary>
        /// <returns></returns>
        public static List<Info> Query()
        {
            using (IDbConnection connection = new MySqlConnection(constr))
            {
                return connection.Query<Info>("select * from sdx_admin_action").ToList();
            }
        }    

 

执行结果如下:

技术分享图片

 

以下为源码包含增删改查:

using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Data;
using Dapper;
using System.Linq;

namespace SelectAll
{
    class Info
    {
        public int action_id { get; set; }
        public int parent_id{ get; set; }
        public string action_code { get; set; }
        public string relevance { get; set; }
    }
    class Program
    {
        //数据库连接配置信息;
        public static string constr = "server = localhost;User Id = root;password = 123456;Database = ecshop";

        static void Main(string[] args)
        {
            /*Info One = new Info();
            One.parent_id = 2;
            One.action_code = "小弱鸡";
            One.relevance = "here~";*/

            //插入
            //Insert(SetInfo(3,"小弱鸡三号","hahahh"));

            //批量插入
            /*List<Info> Infos = new List<Info>();
            Infos.Add(SetInfo(4, "小弱鸡四号", "sdfeff"));
            Infos.Add(SetInfo(5, "小弱鸡五号", "sdfsdfdf"));
            Infos.Add(SetInfo(6, "小弱鸡六号", "sdfe多维ff"));

            Insert(Infos);*/

            //删除;
            //Delete(SetInfo(2, "小弱鸡", "here~"));

            //更新
            //Update(SetInfo(2, "小弱鸡二号·改", "hahahh"));

            List<Info> test = Query();
            string TestInfo = "";

            for (int i = 0; i < test.Count; i++)
            {
                TestInfo = test[i].action_id.ToString() + " , " + test[i].parent_id + " , " + test[i].action_code + " , " + test[i].relevance;
                Console.WriteLine(TestInfo);
            }

            Console.ReadLine();
        }

        /// <summary>
        /// 无条件查询所有;
        /// </summary>
        /// <returns></returns>
        public static List<Info> Query()
        {
            using (IDbConnection connection = new MySqlConnection(constr))
            {
                return connection.Query<Info>("select * from sdx_admin_action").ToList();
            }
        }

        /// <summary>
        /// 设置Info对象并返回
        /// </summary>
        /// <param name="var_parent_id"></param>
        /// <param name="var_action_code"></param>
        /// <param name="var_relevance"></param>
        /// <returns></returns>
        public static Info SetInfo(int var_parent_id,string var_action_code, string var_relevance)
        {
            Info tmp = new Info();
            tmp.parent_id = var_parent_id;
            tmp.action_code = var_action_code;
            tmp.relevance = var_relevance;
            return tmp;
        }

        /// <summary>
        /// 执行插入,插入单条信息;
        /// </summary>
        /// <returns></returns>
        public static int Insert(Info varInfo)
        {
            using (IDbConnection connection = new MySqlConnection(constr))
            {
                return connection.Execute(
                    "insert into sdx_admin_action(parent_id,action_code,relevance) values(@parent_id,@action_code,@relevance)", varInfo);
            }
        }

        /// <summary>
        /// 批量执行插入;
        /// </summary>
        /// <param name="varInfo"></param>
        /// <returns></returns>
        public static int Insert(List<Info> varInfo)
        {
            using (IDbConnection connection = new MySqlConnection(constr))
            {
                return connection.Execute(
                    "insert into sdx_admin_action(parent_id,action_code,relevance) values(@parent_id,@action_code,@relevance)",varInfo
                    );
            }
        }

        /// <summary>
        /// 删除指定条件的行信息;
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static int Delete(Info info)
        {
            using (IDbConnection connection = new MySqlConnection(constr))
            {
                return connection.Execute(
                    "delete from sdx_admin_action where action_code=@action_code",info
                    );
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static int Update(Info info)
        {
            using (IDbConnection connection = new MySqlConnection(constr))
            {
                return connection.Execute(
                    "Update sdx_admin_action set action_code=@action_code where parent_id=@parent_id",info
                    );
            }
        }
    }
}

参考文档:https://www.jianshu.com/p/c4ca2989d26a

在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查

标签:data   rgs   分享图片   host   int   sum   summary   database   条件   

原文地址:https://www.cnblogs.com/YeYunRong/p/8550718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!