78 lines
2.1 KiB
C#
78 lines
2.1 KiB
C#
|
||
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);
|
||
}
|
||
}
|
||
} |