SqlSugar库操作mysql数据库方法
添加引用:
MySql.Data.dll,SqlSugar.dll
增删改查
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SqlSugar_MysqlTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_mysql_test_Click(object sender, EventArgs e)
{
DBContext aa= DBContext.OpDB();
Expression < Func> cd = null;
string insert_str = "sn_202207043";
// List list=aa.GetList(cd=f=>f.id=="10009"&&f.lot=="123456"); //多条件查询
//查询
List list = aa.GetList(cd = f => f.sn_box== insert_str);
for (int i=0;i0)
{
// MessageBox.Show("已经存在物流码"+ insert_str);
// return;
}
//by txwtech
else
{
//增加
// aa.Insert(new Sugar_Get_Info_Class { id = "10011", lot = "", sn_box = insert_str });
}
//删除
aa.Delete(cd = f => f.id == "10008");
//public virtual bool Update(Expression columns, Expression whereExpression);
// Update(Expression < Func < T, T >> 执行的动作,返回的结果类型
//Update(Expression 执行的动作,返回的结果类型
//修改
aa.Update(p=>new Sugar_Get_Info_Class {base_data="99.0",lot="LA123456" },cd=f=>f.id=="10009");
}
}
}
DBContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
//using AgvDisPatch.Config;
//by txwtech
using SqlSugar;
namespace SqlSugar_MysqlTest
{
public class DBContext where T : class, new()
{
public SqlSugarClient Db;
///
/// 单列模式
///
private static DBContext mSingle = null;
public static DBContext GetInstance()
{
if (mSingle == null)
mSingle = new DBContext();
return mSingle;
}
///
/// 修改后的代码
///
///
public static DBContext OpDB()
{
DBContext dbcontext_t = new DBContext();
dbcontext_t.Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "database='" + "txw_oqa_db" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "87958868" + "';charset='utf8';pooling=true",
//DbType = SqlSugar.DbType.MySql,
//IsAutoCloseConnection = true,
//InitKeyType = InitKeyType.Attribute
// ConnectionString = ConfigFile.DataConnString,
DbType = SqlSugar.DbType.MySql,//我这里使用的是Mysql数据库
IsAutoCloseConnection = true,//自动关闭连接
InitKeyType = InitKeyType.Attribute
});
return dbcontext_t;
}
protected DBContext()
{ //通过这个可以直接连接数据库
Db = new SqlSugarClient(new ConnectionConfig()
{
// "database='" + "return" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "root" + "';charset='utf8';pooling=true",
//可以在连接字符串中设置连接池pooling=true;表示开启连接池
//eg:min pool size=2;max poll size=4;表示最小连接池为2,最大连接池是4;默认是100
ConnectionString = "database='" + "txw_oqa_db" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "87958868" + "';charset='utf8';pooling=true",
// ConnectionString = ConfigFile.DataConnString,
DbType = SqlSugar.DbType.MySql,//我这里使用的是Mysql数据库
IsAutoCloseConnection = true,//自动关闭连接
InitKeyType = InitKeyType.Attribute
});
}
public void Dispose()
{
if (Db != null)
{
Db.Dispose();
}
}
public SimpleClient CurrentDb { get { return new SimpleClient(Db); } }
///
/// 获取所有
///
///
public virtual List GetList()
{
return CurrentDb.GetList();
}
///
/// 根据表达式查询
///
///
public virtual List GetList(Expression whereExpression)
{
//db.Queryable().Where(it => it.Name != null).ToList()
return CurrentDb.GetList(whereExpression);
}
///
/// 根据表达式查询分页
///
///
public virtual List GetPageList(Expression whereExpression, PageModel pageModel)
{
return CurrentDb.GetPageList(whereExpression, pageModel);
}
///
/// 根据表达式查询分页并排序
///
/// it
///
/// it=>it.id或者it=>new{it.id,it.name}
/// OrderByType.Desc
///
public virtual List GetPageList(Expression whereExpression, PageModel pageModel, Expression orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
{
return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType);
}
///
/// 根据主键查询
///
///
public virtual List GetById(dynamic id)
{
return CurrentDb.GetById(id);
}
///
/// 根据主键删除
///
///
///
public virtual bool Delete(dynamic id)
{
if (string.IsNullOrEmpty(id.ObjToString))
{
Console.WriteLine(string.Format("要删除的主键id不能为空值!"));
}
return CurrentDb.Delete(id);
}
///
/// 根据实体删除
///
///
///
public virtual bool Delete(T data)
{
if (data == null)
{
Console.WriteLine(string.Format("要删除的实体对象不能为空值!"));
}
return CurrentDb.Delete(data);
}
///
/// 根据主键删除
///
///
///
public virtual bool Delete(dynamic[] ids)
{
if (ids.Count() 0;
}
///
/// 根据表达式删除
///
///
///
public virtual bool Delete(Expression whereExpression)
{
return CurrentDb.Delete(whereExpression);
}
///
/// 根据实体更新,实体需要有主键
///
///
///
public virtual bool Update(T obj)
{
if (obj == null)
{
Console.WriteLine(string.Format("要更新的实体不能为空,必须带上主键!"));
}
return CurrentDb.Update(obj);
}
///
///批量更新
///
///
///
public virtual bool Update(List objs)
{
if (objs.Count
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?