程序和测试数据的github下载地址:https://github.com/yangwohenmai/TEST/tree/master/DapperDB
项目这里要引用一个System.Data.SQLite.dll 一个Dapper.dll
链接数据库方法如下,这里数据库用的是轻量级的sqlite,方便我上传数据库给大家测试用
///
/// 链接数据库
///
///
///
public static IDbConnection GetConnection(string connConfig)
{
IDbConnection conn = null;
if (string.IsNullOrEmpty(connConfig))
{
return null;
}
try
{
conn = new SQLiteConnection(connConfig);
//打开数据库链接
conn.Open();
}
catch (Exception ex)
{
}
return conn;
}
定义一个查询数据库方法
///
/// ORM数据映射
///
///
///
///
public static IEnumerable Select(string sql, string connConfig)
{
IEnumerable list = null;
using (IDbConnection conn = GetConnection(connConfig))
{
try
{
//传统sql in (1, 2, 3)写法
//conn.Query("SELECT * FROM TestTable WHERE id IN (@ids) ",new { ids = IDs.ToArray()})
list = conn.Query(sql);
//list = conn.Query(sql, new { id = ID });
}
catch (Exception ex)
{
}
}
return list;
}
这是调用查询条件,这里数据库用的是文本级别的SQLite数据库,数据库我已经打包在程序里了
///
/// 获取数据
///
///
public static List GetTask()
{
List tempDataList = new List();
string errorMsg = string.Empty;
string sql = @"SELECT * FROM TaskStatus;";
string str_ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "finchina.db3");
string SqlitePath = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", str_ConfigFilePath);
var list = Select(sql, SqlitePath);
if (!string.IsNullOrEmpty(errorMsg))
{
return null;
}
foreach (TaskDriverStru item in list)
{
tempDataList.Add(item);
}
return tempDataList;
}
Dapper是一个ORM,自然少不了一个和数据库字段对应的实体类
public class TaskDriverStru
{
///
/// Id
///
public string Id { get; set; }
///
/// 任务ID
///
public string taskId { get; set; }
///
/// 用户名
///
public string userName { get; set; }
///
/// 任务名
///
public string TaskName { get; set; }
///
/// 表名
///
public string Tables { get; set; }
///
/// 任务状态
///
public string taskStatus { get; set; }
///
/// 时间戳
///
public string TMSP1 { get; set; }
///
/// 时间戳
///
public string TMSP2 { get; set; }
///
/// 时间戳
///
public string TMSP3 { get; set; }
///
/// 是否初始化
///
public bool Init { get; set; }
}
全部代码如下:项目和数据库可以从文章开头给出的github链接上下载
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.IO;
using System.Data.SQLite;
namespace DapperDB
{
class Program
{
static void Main(string[] args)
{
List list = GetTask();
foreach(var item in list)
{
Console.WriteLine(item.Id);
Console.WriteLine(item.Init);
Console.WriteLine(item.Tables);
Console.WriteLine(item.taskId);
Console.WriteLine(item.TaskName);
Console.WriteLine(item.taskStatus);
Console.WriteLine(item.userName);
}
Console.ReadLine();
}
///
/// 获取数据
///
///
public static List GetTask()
{
List tempDataList = new List();
string errorMsg = string.Empty;
string sql = @"SELECT * FROM TaskStatus;";
string str_ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "finchina.db3");
string SqlitePath = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", str_ConfigFilePath);
var list = Select(sql, SqlitePath);
if (!string.IsNullOrEmpty(errorMsg))
{
return null;
}
foreach (TaskDriverStru item in list)
{
tempDataList.Add(item);
}
return tempDataList;
}
///
/// ORM数据映射
///
///
///
///
public static IEnumerable Select(string sql, string connConfig)
{
IEnumerable list = null;
using (IDbConnection conn = GetConnection(connConfig))
{
try
{
//传统sql in (1, 2, 3)写法
//conn.Query("SELECT * FROM TestTable WHERE id IN (@ids) ",new { ids = IDs.ToArray()})
list = conn.Query(sql);
//list = conn.Query(sql, new { id = ID });
}
catch (Exception ex)
{
}
}
return list;
}
///
/// 链接数据库
///
///
///
public static IDbConnection GetConnection(string connConfig)
{
IDbConnection conn = null;
if (string.IsNullOrEmpty(connConfig))
{
return null;
}
try
{
conn = new SQLiteConnection(connConfig);
//打开数据库链接
conn.Open();
}
catch (Exception ex)
{
}
return conn;
}
}
public class TaskDriverStru
{
///
/// Id
///
public string Id { get; set; }
///
/// 任务ID
///
public string taskId { get; set; }
///
/// 用户名
///
public string userName { get; set; }
///
/// 任务名
///
public string TaskName { get; set; }
///
/// 表名
///
public string Tables { get; set; }
///
/// 任务状态
///
public string taskStatus { get; set; }
///
/// 时间戳
///
public string TMSP1 { get; set; }
///
/// 时间戳
///
public string TMSP2 { get; set; }
///
/// 时间戳
///
public string TMSP3 { get; set; }
///
/// 是否初始化
///
public bool Init { get; set; }
}
}