首页 > 其他分享 >Dapper CURD

Dapper CURD

时间:2024-03-17 11:22:06浏览次数:21  
标签:MName CURD Movies connection result new Dapper Id

#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

标签:MName,CURD,Movies,connection,result,new,Dapper,Id
From: https://www.cnblogs.com/jigeqiu/p/18078347

相关文章

  • Dapper 经验
     in查询publicasyncTask<IReadOnlyList<ResourceWorkCenterView>>GetResourceWorkCenterViewByWorkCenterIdsAsync(long[]workCenterIds){stringsql="select*fromResourceWorkCenterViewwhereWorkCenterIdin@workCenterIds";......
  • Dapper 查询多个表,映身到对象的实例对象
    publicasyncTask<IEnumerable<SplitMergeOrderSourcePlanOrderView>>GetSplitMergeOrderSourcePlanOrderViewAsync(longsplitMergeOrderId){stringsql="selecta.*,b.*fromPlanOrderViewajoinSplitMergeOrderSourcebona.Id=b.PlanOr......
  • Dapper官方文档(七)【结果之匿名类型,强类型,多映射,多结果,多类型】
    结果匿名描述可以使用扩展方法执行查询并使用动态类型映射结果。匿名类型结果可以从以下扩展方法映射:QueryQueryFirstQueryFirstOrDefaultQuerySingleQuerySingleOrDefault这些扩展方法可以从IDbConnection类型的任意对象中调用。案例-QueryQuery方法可以执行查询......
  • Sitecore Item CURD
    基础CURDDatabasedb=Sitecore.Configuration.Factory.GetDatabase("master");Languagelang=Language.Parse("en");TemplateItemfolderTemplate=db.GetTemplate(" /sitecore/templates/Common/Folder");//获取HomeItemItemhom......
  • MySQL数据表的CURD
    一、数据表的CURD1.create数据创建一个员工表,新建employee表并向表中添加一些记录:创建数据表:createtableemployee(idint,namevarchar(20),sexint,birthdaydate,salarydouble,entry_datedate,resumetext);向数据表中插入数据:insertintoemployeevalues(......
  • MySQL数据库的CURD
    一、数据库的CURD对数据库进行增(Create)、改(Update)、查(Retrieve)、删(Delete)等操作。CREATE{DATABASE|SCHEMA}[IFNOTEXISTS]db_name[create_specification[,create_specification]...]IFNOTEXISTS表示只有数据库不存在的时候才创建,如果存在同名就不再执......
  • dapper实现CURD
    dapper的使用 1、引用包 2、student类[Table("dbo.Student")]publicpartialclassStudent{[Required]publicintId{get;set;}publicstringName{get;set;}publicstringAddress{get;set;}}2、DapperRepository中以下两个......
  • mybatis---CURD
    查询记录<selectid="getUserList"resultType="org.example.pojo.User">select*fromuser.use1</select><selectid="getUserByid"resultType="org.example.pojo.User"parameterType="i......
  • Dapper NetCore 分区实战
    在上一篇中介绍了基于Dapper的NetCore分表,本篇旨在介绍基于Dapper的NetCore分区,废话不多说开搞吧!模拟业务场景:基于公司所在地区对表建立分区设计公司表结构,其中TableAttribute标识表名,PartitionAttribute标识当前表是分区结构,Property代表按照某个属性分区publicabstractc......
  • Dapper QueryMultiple throws "No columns were selected"
    调试存储过程发现某字段NULL,拼成的SQL最后啥也没有了。。。原因是存储过程没有执行到查询SQL语句QueryAsyncthrows"Nocolumnswereselected"whenstoredprocdoesn'tperformaselectstatement #591Closed  toannguyen83 openedthisissue onAug27,20......