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 =@"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.IsConsumed reader的状态 ,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 Title=@title where Id=@id", 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

Console.ReadKey();
}
}
}

posted @ 2018-06-07 16:45  南国之恋  阅读(145)  评论(0编辑  收藏  举报