[C#]Dapper学习笔记

1.安装,直接用nuget搜索Dapper就行,不过只支持框架4.5.1

2.数据库测试表

CREATE TABLE [dbo].[Student](
    [ID] [bigint] NULL,
    [Name] [nvarchar](50) NULL,
    [Birthday] [date] NULL,
    [TeacherID] [bigint] NULL,
    [Level] [smallint] NULL,
    [Remark] [nvarchar](50) NULL
) ON [PRIMARY]

GO
CREATE TABLE [dbo].[Teacher](
    [ID] [bigint] NULL,
    [Name] [nvarchar](50) NULL,
    [Birthday] [date] NULL,
    [Gender] [bit] NULL
) ON [PRIMARY]

3.Person

   class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

4.Student

 class Student:Person
    {
    
        /// <summary>
        /// 这里故意加了加了个一个实体Teacher,在数据库里面,只有一个TeacherID
        /// </summary>
        public Teacher Teacher{set;get;}

        public int TeacherID
        {
            set;
            get;
        }
        public int? Level{set;get;}

        public string Remark{set;get;}

    }

5.Teacher

   class Teacher:Person
    {
        public bool Gender { get; set; }
    }

6.测试代码

 class Program
    {
        static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString);
        static void Main(string[] args)
        {
            string sql = string.Empty;
            {
                //普通插入
                sql = "insert into Teacher values(@ID,@Name,@Birthday,@Gender) ";
                var max = db.Query<int>("select  isnull(max(id),0) id from Teacher");
                var maxID = max == null ? 1 : max.FirstOrDefault();
                Teacher t = new Teacher() { ID = (maxID + 1), Gender = false, Name = "Miss Gao", Birthday = DateTime.Now.AddYears(-34) };
                var result = db.Execute(sql, t);
            }

            {
                // //普通插入
                var max = db.Query<int>("select isnull(max(id),0) id from student");
                var maxID = max == null ? 1 : max.FirstOrDefault();
                sql = "insert into student values(@ID,@Name,@Birthday,@TeacherID,@Level,@Remark)";
                var result = db.Execute(sql, new Student() { ID = (maxID + 1), Name = "Jim", TeacherID = 1, Remark = "a", Birthday = DateTime.Now, Level = 1 });

            }
            {//无条件查询
                var result1 = db.Query<Student>("Select * from Student");//
                var result2 = db.Query<Student>("Select [ID]      ,[Name]      ,[Birthday]      ,[TeacherID]      ,[Level]      ,[Remark] from Student");//
            }

            {//带条件查询
                var result1 = db.Query<Student>("Select * from Student where name=@Name", new Student() { Name = "Jim" });//
                var result2 = db.Query<Student>("Select * from Student where id=@ID", new Student() { ID = 2 });//
                var result3 = db.Query<Student>("Select [ID]      ,[Name]      ,[Birthday]      ,[TeacherID]      ,[Level]      ,[Remark] from Student", new { Name = "Jim" });//
            }

            {//联合查询
                sql = @"Select *
                from Student a join teacher b on a.TeacherID = b.id
                
                --where a.ID=@ID";//这里加条件怎么办?拼接SQL?
                var result = db.Query<Student, Teacher, Student>(sql, (s, t) => { s.Teacher = t; return s; });
               
            }

            {//联合查询2
                sql = @"Select * from Student where id=@ID ;
                            select * from teacher where ID = (select teacherid from student where id=@ID)";
                var result = db.QueryMultiple(sql, new { ID=5});
                var s = result.Read<Student>().FirstOrDefault();
                s.Teacher = result.Read<Teacher>().FirstOrDefault();
            }

            {//联合查询3
                sql = @"Select * from Student where id=@ID ;
                            select * from teacher where Name =@Name";
                var result = db.QueryMultiple(sql, new { ID = 5,Name="Miss Gao" });//这里的参数 是很奇怪的哦
                var s = result.Read<Student>().FirstOrDefault();
                var t= result.Read<Teacher>().FirstOrDefault();
            }
        }
    }

 

posted @ 2019-04-04 14:10  瘦馬  阅读(227)  评论(1编辑  收藏  举报