#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