首页 > 数据库 >使用SqlSugar操作MySQL/SQL Server数据库

使用SqlSugar操作MySQL/SQL Server数据库

时间:2024-07-18 09:55:10浏览次数:9  
标签:string await private Server CnDB SQL MySQL new public

一、框架简介

SqlSugar 是一款 老牌 .NET开源ORM框架,由果糖大数据科技团队维护和更新 ,开箱即用最易上手的ORM 

优点 :【生态丰富】【高性能】【超简单】 【功能全面】 【多库兼容】【适合产品】

 

二. 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",
             //DbType = DbType.SqlServer,
            //    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> where T : class, new()
{
    private static T _instance;
 
    private static readonly object syslock = new object();
 
    public static T Instance()
    {
        if (_instance == null)
        {
            lock (syslock)
            {
                if (_instance == null)
                {
                    _instance = new 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; }
        }
 
 
    }

六、使用

bool blRet = await MySqlCNHelper.Instance().InsertRecordProModel(model);

var List1 = await  MySqlCNHelper.Instance().QueryDataList();

var List2 = await  MySqlCNHelper.Instance().QueryAllotModelList(model);

bool blRet = await  MySqlCNHelper.Instance().QueryUserModelIsExists(model);

标签:string,await,private,Server,CnDB,SQL,MySQL,new,public
From: https://blog.csdn.net/a876106354/article/details/140514290

相关文章

  • Ubuntu-server 安装网卡驱动
    适用情况网卡缺少驱动,需要安装并启用(以网卡I219-LMRJ15为例) 准备工作:需要安装make​、gcc​需要从官网下载源码安装过程:首先解压.tar文件解压并重命名,移动到指定路径后,编译驱动: ​sudotarzxfe1000e-3.8.4.tar.gz&&sudomve1000e-3.8.4e1000e&&sudomve1......
  • 攻防世界supersqli
    supersqliSQL注入的常规思路是判断注入点,尝试后发现注入点为’判断字符数量得出数量为2然后尝试联合注入发现select等字符被过滤,尝试堆叠注入-1';showdatabases;#猜测在supersqli的可能性会大一点(也可以挨个尝试)-1';usesupersqli;showtables;#查询具体表-......
  • go embed http server
    packagemainimport( "embed" "io/fs" "net/http")//go:embedall:distvarassetsembed.FSfuncAssets()(fs.FS,error){ returnfs.Sub(assets,"dist")}funcmain(){ assets,_:=Assets() //Usethefil......
  • 用php编写代码,实现Linux系统下源码安装Apache、Mysql、PHP以及LAMP部署验证的过程
    以下是使用PHP编写的代码示例,用于自动化安装和部署LAMP环境:<?php//定义安装路径和版本号$apacheVersion='2.4.41';$mysqlVersion='8.0.19';$phpVersion='7.4.4';$apacheInstallPath='/usr/local/apache';$mysqlInstallPath='/usr/local/my......
  • unity3d sqlite
     usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingMono.Data.Sqlite;//注意:这取决于你使用的SQLite库publicclassSQLiteExample:MonoBehaviour{//数据库文件路径privatestringdbPath="URI=file:"+Applicatio......
  • 【第4章】Spring Cloud之Nacos单机模式支持mysql
    文章目录前言一、初始化1.初始化数据库2.修改配置文件二、效果1.重新启动2.新增用户总结前言在0.7版本之前,在单机模式时nacos使用嵌入式数据库实现数据的存储,不方便观察数据存储的基本情况。0.7版本增加了支持mysql数据源能力,具体的操作步骤:安装数据库,版本要......
  • [Mysql]next-key lock
    next-keylock这一节我们通过实验验证next-key-lock在各种情况下的表现:在唯一索引上的等值查询我们利用主键展现这一特性命中记录下面我们来解释这张图发生了什么,会话A会话B1开启事务开启事务2快照读整张表,查到所有数据3对id为10的数据进行加forup......
  • 云计算实训07——搭建ssh服务、创建用户并授权、在RealServer创建code账号、SSH认证原
    一、搭建ssh服务1.安装ssh服务yum-yinstallopensshyum-yinstallssh-serveryum-yinstallssh-client2.关闭防火墙和selinux#关闭防⽕墙(临时)systemctlstopfirewalld#关闭开机⾃启动systemctldisablefirewalld#关闭selinux(临时)sete......
  • 常见的SQL数值型数据处理函数
    在数据驱动的时代,SQL已成为数据分析和管理中不可或缺的工具。无论是处理简单的查询还是复杂的数据分析,SQL都能帮助我们高效地完成任务。然而,在处理数值型数据时,你是否感到过困惑,不知道如何运用SQL中的各种函数来实现数据处理? 究竟有哪些常见的SQL数值型数据处理函数,它......
  • postgresql删除用户
    背景**角色与用户**:在PostgreSQL中,用户和组的概念是通过“角色”来统一实现的。角色可以有登录权限(在这种情况下,它们通常被称为“用户”),也可以没有(在这种情况下,它们通常用于权限管理,类似于组)。**依赖关系**:在删除角色之前,需要确保该角色没有被其他数据库对象(如表、视图、......