Dapper的简单使用(初学者归纳)

2018-06-23 23:59:04来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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 id=@Id";
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 Name=@Name,Pwd=@Pwd,Age=@Age where Id=@Id";
int q = con.Execute(sql, qq);
return q;
}
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:浅谈.NET,C#三层架构(自己总结)

下一篇:使用XmlReader读Xml