五陵年少金市东,银鞍白马渡春风。这篇文章主要讲述Dapper SQL相关的知识,希望能为你提供帮助。
using System;
using Dapper;
using System.Data.SqlClient;
using LoTDapper.Model;
using System.Linq;
using System.Data;
namespace LoTDapper
{
class Program
{
//项目中建议尽量用强类型,虽然麻烦点,但后期好维护
static void Main(string[] args)
{
string connStr [email
protected]"Data Source=WIN-V381VBUULC9\SQL2008;
Initial Catalog=DapperDB;
User ID=sa;
Password=yadan";
#region 强类型
//public static IEnumerable<
T>
Query<
T>
(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
//无参查询
//var qqModeList = conn.Query<
QQModel>
("select Id,Name,Count from QQModel");
//带参查询
var qqModeList = conn.Query<
QQModel>
("select Id,Name,Count from QQModel where Id in @id and Count>
@count", new { id = new int[] { 1, 2, 3, 4, 5, 6 }, count = 1 });
foreach (var item in qqModeList)
{
Console.WriteLine(item.Id + " " + item.Name + " " + item.Count);
}
}
#endregion
#region 动态类型
//逆天动态类型用的比较多[可能是MVC ViewBag用多了]
//public static IEnumerable<
dynamic>
Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
var qqModeList = conn.Query("select Id,Name,Count from QQModel").ToList();
foreach (var item in qqModeList)
{
Console.WriteLine(item.Id + " " + item.Name + " " + item.Count);
}
}
#endregion
#region 多映射
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//string sqlStr = @"select A.Id,A.Title,S.SeoKeywords from Article A
//inner join SeoTKD S on A.SeoId=S.Id
//where A.Id in @ids";
//conn.Open();
//var articleList = conn.Query(sqlStr, new { ids = new int[] { 41, 42, 43, 44, 45, 46, 47, 48 } });
//foreach (var item in articleList)
//{
//Console.WriteLine(item.Id + " | " + item.SeoKeywords + " | :" + item.Title);
//}
//}
#endregion
#region 多返回值
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//string sqlStr = @"select Id,Title,Author from Article where Id = @id
//select * from QQModel where Name = @name
//select * from SeoTKD where Status = @status";
//conn.Open();
//using (var multi = conn.QueryMultiple(sqlStr, new { id = 11, name = "打代码", status = 99 }))
//{
////multi.IsConsumedreader的状态 ,true 是已经释放
//if (!multi.IsConsumed)
//{
////注意一个东西,Read获取的时候必须是按照上面返回表的顺序 (article,qqmodel,seotkd)
////强类型
//var articleList = multi.Read<
Temp>
();
//类不见得一定得和表名相同
//var QQModelList = multi.Read<
QQModel>
();
//var SeoTKDList = multi.Read<
SeoTKD>
();
////动态类型
////var articleList = multi.Read();
////var QQModelList = multi.Read();
////var SeoTKDList = multi.Read();
//#region 输出
//foreach (var item in QQModelList)
//{
//Console.WriteLine(item.Id + " " + item.Name + " " + item.Count);
//}
//foreach (var item in SeoTKDList)
//{
//Console.WriteLine(item.Id + " | " + item.SeoKeywords);
//}
//foreach (var item in articleList)
//{
//Console.WriteLine(item.Author);
//}
//#endregion
//}
//}
//}
#endregion
#region 增删改等
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//conn.Open();
////增
//int count = conn.Execute("insert into Article values(@title,@content,@author,961,1,2,2,N‘2015-11-23 11:06:36.553‘,N‘2015-11-23 11:06:36.553‘,N‘5,103,113‘,91,N‘3,5,11‘,0,N‘/Images/article/16.jpg‘)", new { title = "Title1", content = "TContent1", author = "毒逆天" });
////改
////int count = conn.Execute("update Article set [email
protected] where [email
protected]", new { title = "么么哒", id = 274 });
//if (count >
0)
//{
//Console.WriteLine(count + "条操作成功");
//}
//}
#endregion
#region 存储过程
////查询
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//conn.Open();
////参数名得和存储过程的变量名相同(参数可以跳跃传,键值对方式即可)
////动态类型
////var list = conn.Query("usp_test", new { aId = 11 }, commandType: CommandType.StoredProcedure);
////强类型
//var list = conn.Query<
TitleAndKeyWords>
("usp_test", new { aId = 11 }, commandType: CommandType.StoredProcedure);
//foreach (var item in list)
//{
//Console.WriteLine(item.Id + " | " + item.SeoKeywords + " | :" + item.Title);
//}
//}
////插入
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//conn.Open();
//int count = conn.Execute("usp_insertArticle", new { title = "Title11", content = "TContent1", author = "毒逆天" }, commandType: CommandType.StoredProcedure);
//if (count >
0)
//{
//Console.WriteLine(count + "条操作成功");
//}
//}
////更新
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//conn.Open();
//int count = conn.Execute("usp_updateArticle", new { id = 276, title = "Dapper使用" }, commandType: CommandType.StoredProcedure);
//if (count >
0)
//{
//Console.WriteLine(count + "条操作成功");
//}
//}
#endregion
【Dapper SQL】Console.ReadKey();
}
}
}
推荐阅读
- 不错的Web Application stress
- dapper Oracle
- Java中session与application的异同
- ASTC on Android
- uniapp点击底部tabbar不跳转页面
- java中session和application的用法
- 混合模式为什么成为占有率最高的app开发技术
- react native 适配安卓全面屏手机
- 重温Android和Fragment生命周期