• Dapper的使用


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Dapper;
    using Model;
    
    namespace DAL
    {
        public class Dbhelper
        {
            
            private static readonly string con = "Data Source=.;Initial Catalog=Shanke_DB;Integrated Security=True";
    
            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="user"></param>
            /// <returns></returns>
            public static int Insert(UserInfo user)
            {
                using (IDbConnection conn = new SqlConnection(con))
                {
                    return conn.Execute("insert into UserInfo(User_Name,User_Sex,User_Phone,User_Hobby) value(@User_Name,@User_Sex,@User_Phone,@User_Hobby)", user);
                }
            }
            //查询
            public static List<UserInfo> Query()
            {
                using (IDbConnection conn = new SqlConnection(con))
                {
                    return conn.Query<UserInfo>("select * from UserInfo").ToList();
                }
            }
            /// <summary>
            /// 带条件查询
            /// </summary>
            /// <param name="user"></param>
            /// <returns></returns>
            public static UserInfo Query(UserInfo user)
            {
                using (IDbConnection conn = new SqlConnection(con))
                {
                    return conn.Query<UserInfo>("select * from UserInfo where User_Name=@User_Name",user).SingleOrDefault();
                }
            }
    
            //删除
            public static int Delete(UserInfo user)
            {
                using (IDbConnection connection = new SqlConnection(con))
                {
                    return connection.Execute("delete from UserInfo where Id=@Id", user);
                }
            }
    
            public static int Delete(List<UserInfo> user)
            {
                using (IDbConnection connection = new SqlConnection(con))
                {
                    return connection.Execute("delete from UserInfo where Id=@Id", user);
                }
            }
    
            public static int Update(UserInfo user)
            {
                using (IDbConnection connection = new SqlConnection(con))
                {
                    return connection.Execute("update UserInfo set User_Name=@User_Name where Id=@Id", user);
                }
            }
            //修改
            public static int Update(List<UserInfo> user)
            {
                using (IDbConnection connection = new SqlConnection(con))
                {
                    return connection.Execute("update UserInfo set User_Name=@User_Name where Id=@Id", user);
                }
            }
        }
    }
  • 相关阅读:
    第五次作业
    第四次作业
    第三次
    request.getAttribute()和 request.getParameter()有何区别
    .Servlet API中forward()与redirect()的区别
    jsp和servlet的区别、共同点、各自应用的范围
    Servlet的生命周期
    如何从CDN加载jQuery
    window.onload()函数和jQuery中的document.ready()的区别
    jquery中$.get()提交和$.post()提交的区别
  • 原文地址:https://www.cnblogs.com/wasdkoo/p/13265088.html
Copyright © 2020-2023  润新知