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); } } } }