dapper orm

 using Dapper;

 

下面的代码可以作为使用参考:

public static readonly string sqlconnectionString = "Data Source=xxx;Initial Catalog=Express;User ID=sa;Password=123";

 
public static readonly string mysqlconnectionString = @"server=xxx;database=dddd;uid=xxx;pwd=123;charset='gbk'";
 
public static SqlConnection SqlConnection()
{
    var connection = new SqlConnection(sqlconnectionString);
    connection.Open();
    return connection;
}
 
public static  MySqlConnection  MySqlConnection()
{
    var connection = new MySqlConnection(mysqlconnectionString);
    connection.Open();
    return connection;

}

调用方法

 

 

SqlConnection connection = Program.SqlConnection();

 

 

获得一个实体对象

var d = connection.Query<Dog>("select * from dog where id = 1"null).Single<Dog>();

获得实体对象结合

var dd = connection.Query<Dog>("select * from dog where id < 10", null).ToList<Dog>();

插入数据

复制代码
//动态参数
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",
new { @age = i,@name = Guid.NewGuid().ToString(), @Weight = i });

//直接传入实体
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",model);
复制代码

 

复制代码
Execute a query and map the results to a strongly typed List

Note: all extension methods assume the connection is already open, they will fail if the connection is closed.

public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

Example usage:

publicclassDog
{
    publicint?Age{get;set;}
    publicGuidId{get;set;}
    publicstringName{get;set;}
    publicfloat?Weight{get;set;}

    publicintIgnoredProperty{get{return1;}}
}            
            
var guid =Guid.NewGuid();
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id",new{Age=(int?)null,Id= guid });
            
dog.Count()
    .IsEqualTo(1);

dog.First().Age
    .IsNull();

dog.First().Id
    .IsEqualTo(guid);

Execute a query and map it to a list of dynamic objects

public static IEnumerable<dynamic> Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

This method will execute SQL and return a dynamic list.

Example usage:

 var rows = connection.Query("select 1 A, 2 B union all select 3, 4");

((int)rows[0].A)
   .IsEqualTo(1);

((int)rows[0].B)
   .IsEqualTo(2);

((int)rows[1].A)
   .IsEqualTo(3);

((int)rows[1].B)
    .IsEqualTo(4);

Execute a Command that returns no results

public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)

Example usage:

connection.Execute(@"
  set nocount on 
  create table #t(i int) 
  set nocount off 
  insert #t 
  select @a a union all select @b 
  set nocount on 
  drop table #t",new{a=1, b=2})
   .IsEqualTo(2);

Execute a Command multiple times

The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)

Example usage:

connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[]{new{ a=1, b=1},new{ a=2, b=2},new{ a=3, b=3}}
  ).IsEqualTo(3);// 3 rows inserted: "1,1", "2,2" and "3,3"

This works for any parameter that implements IEnumerable<T> for some T.
复制代码

 

 

 

 

var dd = connection.Query<Dog>("select * from dog where id < 10"null).ToList<Dog>();

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值