C#如何使用SqlSugar操作MySQL/SQL Server数据库

本文主要是介绍C#如何使用SqlSugar操作MySQL/SQL Server数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一. SqlSugar 连接MySQL数据库

 public class MySqlCNHelper : Singleton<MySqlCNHelper>{public static SqlSugarClient CnDB;public void InitDB() {//--------------------MySQL--------------------CnDB = new SqlSugarClient(new ConnectionConfig(){ConnectionString = "server=127.0.0.1;uid=root;pwd=zhumengxy;database=fish",DbType = DbType.MySql,IsAutoCloseConnection = true,InitKeyType = InitKeyType.Attribute });//--------------------SQL Server--------------------//mDB = new SqlSugarClient(new ConnectionConfig()//{//    ConnectionString = "Data Source=your_server_name;Initial                            //    Catalog=your_database_name;User                 //          ID=your_username;Password=your_password",//    IsAutoCloseConnection = true,  // 查询完毕后自动关闭连接//})CnDB.Aop.OnLogExecuting = (sql, pars) =>{Console.WriteLine(sql + "\r\n" +mDB.Utilities.SerializeObject(pars.ToString()));Console.WriteLine();};}
}

二.  数据库操作

//插入
public async Task<bool> InsertRecordProModel(RecordProModel recordPro) => await CnDB.Insertable(recordPro).ExecuteCommandAsync() > 0;//查询1
public async Task<List<T>> QueryDataList<T>() => await CnDB.Queryable<T>().ToListAsync();//查询2
public async Task<List<AllotModel>> QueryAllotModelList(AllotModel allot) => await CnDB.Queryable<AllotModel>().Where(t => t.EsamNo == allot.EsamNo || t.ProdOrderNo == allot.ProdOrderNo).ToListAsync();//查询3
public async Task<bool> QueryUserModelIsExists(UserModel user) => await CnDB.Queryable<UserModel>().Where(t => t.UserName == user.UserName && t.PassWord == user.PassWord).AnyAsync();//删除
public async Task<bool> DeleteRecordProModelBW(int id) => await CnDB.Deleteable<RecordProModel>().Where(t => t.ID == id).ExecuteCommandAsync() > 0;//更新
public async void UpdateData(int id ,string esamNo)
{AllotModel userData = await CnDB.Queryable<AllotModel>().Where(t => t.ID == id).FirstAsync();if (userData != null){userData.EsamNo = esamNo;await CnDB.Updateable(userData).ExecuteCommandAsync();}
}

三.  Singleton类

public class Singleton<T>
{private static Singleton<T> _instance = null;public Singleton() { }public static Singleton<T> Instance(){if (_instance == null)_instance = new Singleton<T>();return _instance;}
}

四、AllotModel 类

 public class AllotModel{private int Id;public int ID{get { return Id; }set { Id = value;}}private string esamNo = "";public string EsamNo{get { return esamNo; }set { esamNo = value;}}private AppModel appInfo = new AppModel(); public AppModel AppInfo{get { return appInfo; }set { appInfo = value; }}private string cipherTxt = ""; public string CipherTxt{get { return cipherTxt; }set { cipherTxt = value;}}private string repetitions; public string Repetitions{get { return repetitions; }set { repetitions = value;}}private string prodOrderNo = "";public string ProdOrderNo{get { return prodOrderNo; }set { prodOrderNo = value; }}}

五.  使用

MySqlCNHelper  cn = new MySqlCNHelper();

bool blRet = await cn.InsertRecordProModel(model);

var List1 = await  cn.QueryDataList();

var List2 = await  cn.QueryAllotModelList(model);

bool blRet = await  cn.QueryUserModelIsExists(model);

六、完结

这篇关于C#如何使用SqlSugar操作MySQL/SQL Server数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/462431

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Linux中的计划任务(crontab)使用方式

《Linux中的计划任务(crontab)使用方式》:本文主要介绍Linux中的计划任务(crontab)使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言1、linux的起源与发展2、什么是计划任务(crontab)二、crontab基础1、cro

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进