zoukankan      html  css  js  c++  java
  • Dapper入门学习

    今天了解并学习了下Dapper的用法,这里简单介绍下

    1.安装Nuget Dapper Package包

    2.代码

    using (IDbConnection connection = new SqlConnection("server=.;database=Test;uid=sa;pwd=123456"))
                {

                        //通过connection.Execute(sql,object)方法执行增删改
    //UserInfo userInfo = new UserInfo(); //userInfo.Id = Guid.NewGuid(); //userInfo.UserName = "aa"; //userInfo.Name = "张三"; //string sql = "insert into UserInfo values(@id,@userName,@name)"; //connection.Execute(sql, userInfo); //connection.Execute(sql, new //{ // Id = Guid.NewGuid(), // UserName = "bb", // Name = "张三1" //}); //UserInfo userInfo = new UserInfo(); //userInfo.Id = Guid.NewGuid(); //userInfo.UserName = "aa"; //userInfo.Name = "张三"; //Comment comment = new Comment(); //comment.Id = Guid.NewGuid(); //comment.Content = "东西挺好的"; //comment.UserInfoId = userInfo.Id; // string sql = "insert into UserInfo values(@id,@userName,@name)"; //connection.Execute(sql, userInfo); //string sql1 = "insert into Comment values(@id,@Content,@UserInfoId)"; //connection.Execute(sql1, comment); //userInfo.Name = "李四"; //string sqlupdate = "update UserInfo set name = @name where username=@username"; //connection.Execute(sqlupdate, userInfo); //string sqldelete = "delete from userinfo where username = @username"; //connection.Execute(sqldelete, userInfo);          //通过connection.Query(sql)执行查询 // string query = "select * from userinfo"; // var list = connection.Query(query).ToList(); // foreach (dynamic d in list) // { // Console.WriteLine(d.UserName); // } // string query1 = "select * from userinfo where username = @username"; //List<UserInfo> userInfos = connection.Query<UserInfo>(query1, new {username = "bb"}).ToList(); // foreach (UserInfo d in userInfos) // { // Console.WriteLine(d.UserName); // } string query = "select * from userinfo u left join comment c on u.id=c.userInfoId"; ////Query<TFirst, TSecond, TReturn> //List<UserInfo> userInfos = connection.Query<UserInfo, Comment, UserInfo>(query, // (userInfo, comment) => // { // if (userInfo.Comments == null) // { // userInfo.Comments = new List<Comment>(); // } // userInfo.Comments.Add(comment); // return userInfo; // } //).ToList();           //通过connection.QueryMultiple(sql)一次查询多个实体 //using (var multi = connection.QueryMultiple(query)) //{ // var userInfoss = multi.Read<UserInfo>().ToList(); // foreach (var userInfo in userInfoss) // { // Console.WriteLine(userInfo.UserName); // } //} //var user = connection.Query<UserInfo>("uspGetUser", new {id = "1A61D9E8-A295-4844-8EB1-3F390657CFBB"}, // commandType: CommandType.StoredProcedure).SingleOrDefault(); //Console.WriteLine(user.Name); Console.ReadKey(); }
  • 相关阅读:
    delphi7 stringgrid 点列头排序
    如何作为一个优秀的ERP实施顾问
    小米生态链去年收入150亿,今年目标200亿
    msys2 安装笔记(可以按照这个关键字搜索)
    MinGW 编译 libsndfile-1.0.25(只要有 MSYS,./configure make make install 就行了)
    Apache Ignite——新一代数据库缓存系统
    VS2015 C#6.0
    Webuploader 大文件分片上传
    requirejs
    require.js
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/5333690.html
Copyright © 2011-2022 走看看