Dapper的简单使用(初学者归纳)
//引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Dapper;
//建立数据库连接
public static SqlConnection sqlconn()
{
string ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
var conntion = new SqlConnection(ConnString);
conntion.Open();
return conntion;
}
//列表
public List<User> GetList()
{
using (IDbConnection con = UserDal.sqlconn())
{
string sqlcommandtex = "select * from tb_User";
List<User> userlist = SqlMapper.Query<User>(con, sqlcommandtex).ToList();
return userlist;
}
}
//删除
public int del(int id)
{
using (IDbConnection con = UserDal.sqlconn())
{
string comm = "delete from tb_User where id = @id";
int res = con.Execute(comm, new { id = id });
return res;
}
}
//参数列表
public User GetUser(int id)
{
using(IDbConnection con = UserDal.sqlconn())
{
string sql = "select * from tb_User where [email protected]";
User q = con.Query<User>(sql, new { id = id }).SingleOrDefault();
return q;
}
}
//修改
public int Upd(User qq)
{
using(IDbConnection con = UserDal.sqlconn())
{
string sql = "update tb_User set [email protected],[email protected],[email protected] where [email protected]";
int q = con.Execute(sql, qq);
return q;
}
}