Dapper.net 入门demo 轻量级ORM框架

1、conn(DAO)项目引用Dapper.dll (测试版本1.50.2.0)

 2、StudentDAO.cs 代码附上

public class StudentDAO
    {
        public List<StudentModel> SearchStudentList()
        {
            List<StudentModel> Studentlist = new List<StudentModel>();
            using (IDbConnection conn = SqlConn.GetSqlConnection())
            {
                string sqlcommandText = "SELECT * FROM dbo.Students ";
                return Studentlist = conn.Query<StudentModel>(sqlcommandText, null).ToList();
            }
        }

        public bool AddList(string name)
        {
            using (IDbConnection conn = SqlConn.GetSqlConnection())
            {
                string sqlcommandText = "insert into dbo.Students values(@Name)";
                return conn.Execute(sqlcommandText, new { Name = name }) > 0 ? true : false;
            }
        }

        public bool UpdateList(int id, string name)
        {
            using (IDbConnection conn = SqlConn.GetSqlConnection())
            {
                string sqlcommandText = "Update dbo.Students set Name = @name where Id = @Id";
                return conn.Execute(sqlcommandText, new { Id = id, Name = name }) > 0 ? true : false;
            }
        }

        public bool DeleteList(int id)
        {
            using (IDbConnection conn = SqlConn.GetSqlConnection())
            {
                string sqlcommandText = "Delete dbo.Students where Id = @Id";
                return conn.Execute(sqlcommandText, new { Id = id }) > 0 ? true : false;
            }
        }

    }
View Code

Conn.cs  代码附上 (配置数据库字符串)

public static class SqlConn
    {
        //配置数据库字符串
        private static string sqlstr = ConfigurationManager.ConnectionStrings["SqlServerDB"].ConnectionString;

        //连接数据库
        public static SqlConnection GetSqlConnection()
        {
            SqlConnection conn = new SqlConnection(sqlstr);
            conn.Open();
            return conn;
        }
    }
View Code

StudentModel.cs 代码附上 (数据库一样的结构,这里就不贴出来)

public class StudentModel
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }

app.config 配置连接字符串

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="SqlServerDB" connectionString="Data Source=DESKTOP-V0V6TJ9\MSSQLSERVER2016;Initial Catalog=TextDB;Persist Security Info=True;User ID=sa;Password=123;Max Pool Size=512;Connection Timeout=60"/>
  </connectionStrings>
</configuration>
View Code

Program.cs 代码附上(测试流程)

class Program
    {
        static void Main(string[] args)
        {
            
            //初始状态
            int status = 0;

            //操作
            while (status >= 0)
            {
                Console.WriteLine("");
                Console.WriteLine("请输入指令:1、查询  2、增加  3、修改  4、删除  5、退出 !");

                int num = Convert.ToInt32(Console.ReadLine());
                switch (num)
                {
                    case 1:
                        //查询方法
                        SearchList();
                        break;
                    case 2:
                        Console.WriteLine("请输入姓名!");
                        string strName = Console.ReadLine();

                        //增加方法
                        AddList(strName);
                        break;
                    case 3:
                        //修改方法
                        Console.WriteLine("请输入要修改的Id!");
                        int id = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("请输入新名字");
                        string name = Console.ReadLine();
                        UpdateList(id, name);
                        break;
                    case 4:
                        //删除方法
                        Console.WriteLine("请输入要删除的Id!");
                        int idcopy = Convert.ToInt32(Console.ReadLine());
                        DeleteList(idcopy);
                        break;
                    case 5:
                        status = -1;
                        break;
                    default:
                        Console.WriteLine("指令输入错误!");
                        break;

                }


            }
            Environment.Exit(0);
        }
        static StudentDAO sddao = new StudentDAO();
        public static void SearchList()
        {
            //查询数据
            List<StudentModel> ListPerson = sddao.SearchStudentList();
            foreach (StudentModel p in ListPerson)
            {
                Console.WriteLine(p.Id + p.Name);
            }
        }

        public static void AddList(string name)
        {
            //添加数据
            sddao.AddList(name);
        }

        public static void UpdateList(int id, string name)
        {
            //修改数据
            if (sddao.UpdateList(id, name))
            {
                Console.WriteLine("修改成功!");
            }
            else
            {
                Console.WriteLine("修改失败!");
            };
        }


        public static void DeleteList(int id)
        {
            //删除数据
            if (sddao.DeleteList(id))
            {
                Console.WriteLine("删除成功!");
            }
            else
            {
                Console.WriteLine("删除失败!");
            };

        }
    }
View Code

 

转载于:https://www.cnblogs.com/MycnBlogs7854/p/8676608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值