不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述在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; } }
文章图片
【在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查】
四、添加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
推荐阅读
- elasticsearch入门使用 Mapping + field type字段类型
- Android面试收集录 2D绘图与动画技术
- vector android:fillType gradient android:endX attribute not found
- [Android Pro]深入理解函数的调用过程——栈帧
- android 自定义Button,抛弃写shape文件
- 安卓开发打造闪光控件效果
- Android面试收集录 电话短信和联系人多媒体技术
- Android Studio 第六十期 - Android推流直播(斗鱼部分页面功能)
- android studio环境搭建笔记