码迷,mamicode.com
首页 > 移动开发 > 详细

Dapper一个轻量级的ORM库

时间:2016-08-02 14:51:50      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

   相关阅读

http://stackexchange.github.io/dapper-dot-net/

https://github.com/StackExchange/dapper-dot-net

https://msdn.microsoft.com/zh-cn/magazine/mt703432.aspx

引用

  可以直接通过Nuget下载,需要源码的话可以通过git去下载,上面已经给出链接

  技术分享

基本操作

  简单的定义了一张mssql数据表

  技术分享

        private static List<Student> Read()
        {
            using (var connect = new SqlConnection(connectionString))
            {
                connect.Open();
                var query = connect.Query<Student>("SELECT TOP 1000 [Id],[Name],[Grade] FROM [TESTDB].[dbo].[Student];");
                return query.ToList();
            }
        }

        private static void Add()
        {
            using (var connect = new SqlConnection(connectionString))
            {
                connect.Open();
                var result = connect.Execute("INSERT INTO [dbo].[Student]([Id],[Name],[Grade])VALUES(@Id,@Name,@Grade);", new[] {
                    new { Id=2,Name="李四",Grade=1},
                    new { Id=3,Name="王五",Grade=1},
                    new { Id=4,Name="赵六",Grade=1}
                });
                Console.WriteLine("影响行数{0}", result);
            }
        }

        private static void Update()
        {
            using (var connect = new SqlConnection(connectionString))
            {
                connect.Open();
                var result = connect.Execute("UPDATE [dbo].[Student]  SET [Grade] = @Grade WHERE Id=@Id; ", new { Id = 2, Grade = 2 });
                Console.WriteLine("影响行数{0}", result);
            }
        }

        private static void Delete()
        {

            using (var connect = new SqlConnection(connectionString))
            {
                connect.Open();
                var result = connect.Execute("DELETE FROM [dbo].[Student] WHERE Id =@Id; ", new { Id = 1 });
                Console.WriteLine("影响行数{0}", result);
            }
        }

Dapper一个轻量级的ORM库

标签:

原文地址:http://www.cnblogs.com/liunlls/p/dapper.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!