在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查
Posted 葉蕓榕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查相关的知识,希望对你有一定的参考价值。
一、首先先创建一个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并执行一些增删改查的主要内容,如果未能解决你的问题,请参考以下文章
Dapper 或 MySql 找不到包含句号“。”的存储过程。