Files
SzmediTools/Szmedi.RvKits/Assists/DapperUtil.cs
2025-09-16 16:06:41 +08:00

78 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Windows;
using Dapper;
namespace Szmedi.RvKits.Assists
{
public class DapperUtil
{
private readonly string connectionString;
private IDbConnection cnn;
public DapperUtil(string dbConnectionString)
{
connectionString = dbConnectionString;
}
public IDbConnection CreateConnect()
{
try
{
cnn = new SQLiteConnection(connectionString);
cnn.Open();
}
catch (Exception e)
{
MessageBox.Show("连接失败", e.Message);
LogAssists.WriteLog(e.StackTrace);
}
return cnn;
}
public int Delete(int id)
{
return cnn.Execute("delete from param where ID=" + id);
}
public List<T> LoadModel<T>(string tableName)
{
var output = cnn.Query<T>("select * from " + tableName, new DynamicParameters());
return output.ToList();
}
public List<T> QueryIn<T>()
{
var sql = "select * from param where id in @ids";
//参数类型是Array的时候dappper会自动将其转化
return cnn.Query<T>(sql, new { ids = new int[2] { 1, 2 } }).ToList();
}
public List<T> QueryIn<T>(int[] ids)
{
var sql = "select * from param where id in @ids";
//参数类型是Array的时候dappper会自动将其转化
return cnn.Query<T>(sql, new { ids }).ToList();
}
public void CloseConnect()
{
cnn.Close();
}
/// <summary>
/// </summary>
/// <param name="param"></param>
/// <param name="id"></param>
/// <param name="sql">"update param set MName = @MName,Age = @Age,Sex = @Sex,Phone = @Phone where ID=" + id</param>
public void UpdateData<T>(T param, string sql)
{
cnn.Execute(sql, param);
}
}
}