#region 使用匿名类插入数据
//IDbConnection connection = new SqlConnection("Data Source=PC20171312\\SQLEXPRESS;Initial Catalog=Users;uid=sa;pwd=sql123;MultipleActiveResultSets=True");
//var result = connection.Execute(
//"Insert into Movies(MName) values (@MName)",
//new { MName = "Tom" });
#endregion
#region 使用实体类插入数据
//string sqlCommandText = "insert into Movies(MName) Values (@MName)";
//using (IDbConnection connection = new SqlConnection(conn))
//{
// Movies user = new Movies()
// {
// MName = "tim"
// };
// int result = connection.Execute(sqlCommandText, user);
//}
#endregion
#region 批量插入
//IDbConnection connection = new SqlConnection(conn);
// var userList = Enumerable.Range(1, 15).Select(i => new Movies()
// {
// MName = i.ToString()
// });
// var result = connection.Execute("insert into Movies(MName) values(@MName)", userList);
// if (result > 0)
// {
// Console.WriteLine("插入成功!");
// }
// else
// {
// Console.WriteLine("插入失败!");
// }
// Console.ReadKey();
#endregion
#region 查询
//IDbConnection connection = new SqlConnection(conn);
//var query = connection.Query<Movies>("SELECT * FROM Movies");
//query.AsList().ForEach(p =>
//{
// Console.WriteLine("Id:" + p.Id + " MName:" +p.MName) ;
//});
//Console.ReadKey();
#endregion
#region 使用匿名类更新
//using (IDbConnection connection = new SqlConnection(conn))
//{
// var result = connection.Execute("update Movies set MName='张三' where Id=@Id", new {Id =304});
//}
#endregion
#region 使用实体类更新
//using (IDbConnection connection = new SqlConnection(conn))
//{
//Movies m = new Movies();
//m.MName = "张无忌";
//m.Id = 287;
//var result = connection.Execute("update Movies set MName=@MName where Id=@Id", m);
//}
#endregion
#region 使用键值对更新
//using (IDbConnection connection = new SqlConnection(conn))
//{
// List<KeyValuePair<string, object>> keys = new List<KeyValuePair<string, object>>();
// keys.Add(new KeyValuePair<string, object>("@MName", "风清扬"));
// keys.Add(new KeyValuePair<string, object>("@Id",286));
// var result = connection.Execute("update Movies set MName=@MName where Id=@Id", keys);
//}
#endregion
#region 使用匿名类删除数据
//using (IDbConnection connection = new SqlConnection(conn))
//{
// var result = connection.Execute("delete from Movies where Id=@Id", new { Id = 284 });
//}
#endregion
#region 使用实体类删除数据
using (IDbConnection connection = new SqlConnection(conn))
{
Movies m = new Movies();
m.Id = 283;
var result = connection.Execute("delete from Movies where Id=@Id", m);
}
#endregion