使用SQLsugar 进行数据库操作
如果使用SQLite,需要下载System.Sqlite, Sqlsugar, newtonsoft.Json官方文档:https://www.bookstack.cn/read/SqlSugar-4.x-zh/fb0c2301c8dfe32c.mdusing SqlSugar;using System;using System.Collections.Generic;using System.L
·
如果使用SQLite,需要下载
System.Sqlite, newtonsoft.Json,Sqlsugar
如果使用MySql,则需要newtonsoft.Json,Sqlsugar, MySql.Data 8.18,mysql的连接可以参照 :https://www.cnblogs.com/ShaYeBlog/p/12667135.html
官方文档:https://www.bookstack.cn/read/SqlSugar-4.x-zh/fb0c2301c8dfe32c.md,可以使用SqlSugar ORM工具箱生成实体类
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class ActualDataProvider
{
public int Delete(ActualData t)
{
if (t == null) return 0;
ActualData model = SqlSugarHelper.db.Queryable<ActualData>().Where(item => item.Id == t.Id).First();
if (model == null) return 0;
int count = SqlSugarHelper.db.Deleteable<ActualData>(model).ExecuteCommand();
return count;
}
//public int Insert(ActualData t)
//{
// int count = SqlSugarHelper.db.Insertable<ActualData>(t).ExecuteCommand();
// return count;
//}
/// <summary>
/// 批量插入
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
///
public int Insert(List<ActualData> t)
{
if (t == null) return 0;
if (t.Value == null) return 0;
方法1
//foreach (var item in t.ToArray())
//{
// SqlSugarHelper.db.Insertable<ActualData>(item).ExecuteCommand();
//}
方法2
int count = SqlSugarHelper.db.Insertable(t.ToArray()).ExecuteCommand();//官方推荐的
return count;
}
public List<ActualData> Select()
{
DateTime day = DateTime.Now.AddDays(-1);
return SqlSugarHelper.db.Queryable<ActualData>().Where(u => SqlFunc.Between(u.InsertTime, day,DateTime.Now)).ToList();
//return SqlSugarHelper.db.Queryable<ActualData>().ToList();
}
public int Update(ActualData t)
{
if (t == null) return 0;
ActualData model = SqlSugarHelper.db.Queryable<ActualData>().Where(item => item.Id == t.Id).First();
if (model == null) return 0;
model.Value = t.Value;
model.Remark = t.Remark;
model.VarName = t.VarName;
int count = SqlSugarHelper.db.Updateable<ActualData>(model).ExecuteCommand();
return count;
}
}
}
using DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SugarShenhua
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
SqlSugarService.SetConnectionStr("Data Source=" + Application.StartupPath + str2);
//WPF
//SqlSugarService.SetConnectionStr("Data Source=" + System.IO.Directory.GetCurrentDirectory() + str2);
}
//"Data Source=192.168.0.15;Initial Catalog=Sugar;User ID=sa;Password=12345678;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
string str2 = "\\Config\\ScadaData.db;Pooling=true;FailIfMissing=false";
string ConnectionString = "database='" + "shenhuadata" + "';Data Source = '" + "localhost" + "'; User Id = '" + "root" + "'; pwd='" + "sasa" + "';charset='utf8';pooling=true"; //mySql
string str3 = "Data Source=.;Initial Catalog=Sugar;User ID=sa;Password=sasa;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
private void Add_Click(object sender, EventArgs e)
{
var t1 = new ActualData()
{
InsertTime = DateTime.Now,
Remark = "测试Remark1",
Value = "测试Value1",
VarName = "测试变量名1"
};
var t2 = new ActualData()
{
InsertTime = DateTime.Now,
Remark = "测试Remark2",
Value = "测试Value2",
VarName = "测试变量名2"
};
var t3 = new ActualData()
{
InsertTime = DateTime.Now,
Remark = "测试Remark3",
Value = "测试Value3",
VarName = "测试变量名3"
};
List<ActualData> actualDatas = new List<ActualData>();
actualDatas.Add(t1);
actualDatas.Add(t2);
actualDatas.Add(t3);
ActualDataProvider actProvider = new ActualDataProvider();
actProvider.Insert(actualDatas);
}
private void Select_Click(object sender, EventArgs e)
{
ActualDataProvider actProvider = new ActualDataProvider();
Dgv.DataSource= actProvider.Select();
}
private void Update_Click(object sender, EventArgs e)
{
ActualDataProvider actProvider = new ActualDataProvider();
var t1 = new ActualData()
{
Id = 38,
InsertTime = DateTime.Now,
Remark = "插入练习1",
Value = "插入练习2",
VarName = "插入练习3"
};
int count = actProvider.Update(t1);
}
private void Delete_Click(object sender, EventArgs e)
{
ActualDataProvider actProvider = new ActualDataProvider();
var t1 = new ActualData()
{
Id = 38,
InsertTime = DateTime.Now,
Remark = "插入练习1",
Value = "插入练习2",
VarName = "插入练习3"
};
int count = actProvider.Delete(t1);
}
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)