支持多数据库的本质是因为Dapper是对IDBConnection接口进行了方法扩展,比如你看到的SqlMapper.cs,一旦你这样做了,我们也知道,
SqlConnection,MysqlConnection,OracleConnection都是继承于DBConnection,而DBConnection又是实现了IDBConnection的接口
Dapper官网:https://code.google.com/p/dapper-dot-net/
Dapper简单使用:http://www.cnblogs.com/wywnet/p/3422150.html
1.通过nuget进行安装 或者 PM->Install-package Dapper
2.在github上获取源码,把Dapper文件夹中的cs放到项目中就行,好像有很多的不能使用,具体不知道为啥,我是使用第一种nuget安装的
注:
我在刚开始装的时候出现了一个问题 “Install-Package : 无法安装程序包“Dapper 1.50.5 ”您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.5”作为目标的项目中,但此程序包不包 含与该框架兼容的任何程序集引用或内容文件”,解决方式将NETFramework,换成4.5.1版本解决了这个问题。如果没有出现这问题,忽略。
public class DapperTest
{
public string ID { get; set; }
public string UserName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
// public DapperTest2 imeinvoice { get; set; }
}
public class DapperTest2
{
public string ID { get; set; }
public string NO { get; set; }
public string METHOD { get; set; }
}
IDbConnection connection = new SqlConnection("Data Source=*******;Initial Catalog=***;
User ID=sa;pwd=pasww;Connection Timeout=600;");
var result = connection.Execute("Insert into DapperTest values (@UserName, @Email, @Address)",
new { UserName = "jack", Email = "[email protected]", Address = "上海" });
var usersList = Enumerable.Range(0, 10).Select(i => new DapperTest()
{
Email = i + "qq.com",
Address = "安徽",
UserName = i + "jack"
});
//var result = connection.Execute("Insert into DapperTest
values (@UserName, @Email, @Address)", usersList);
//2.3.1 无参数
var query = connection.Query("SELECT TOP 100 *FROM dbo.DapperTest ");
//2.3.2有参数
var query11 = connection.Query("SELECT TOP 100 *FROM dbo.DapperTest
where ID=@ID1", new { ID1 = 78 });
//2.3.3多条语句查询
var sql = "select * from DapperTest; select top 100 * from DapperTest2;update DapperTest set
UserName= @UserName where ID=@ID";
var multiReader = connection.QueryMultiple(sql, new { UserName = "1554444", ID = 10 });
var productList = multiReader.Read();
var userList = multiReader.Read();
multiReader.Dispose();
//2.3.4 取一条
var sql1 = @" SELECT top 1 * FROM [dbo].[DapperTest] AS a LEFT JOIN [dbo].[DapperTest2]
AS b ON b.ID=a.ID";
var info1 = connection.QuerySingleOrDefault(sql1); //不加top1 ,如果有多条会报错
//2.3.5 取存储过程
var info1111 = connection.Query("sp_GetUsers",
commandType: CommandType.StoredProcedure);
public class DapperTest
{
public string ID { get; set; }
public string UserName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public DapperTest2 imeinvoice { get; set; }
}
public class DapperTest2
{
public string ID { get; set; }
public string NO { get; set; }
public string METHOD { get; set; }
}
//包含关系查询
var sql1 = @" SELECT * FROM [dbo].[DapperTest] AS a LEFT JOIN [dbo].[DapperTest2] AS b ON b.ID=a.ID";
var info = connection.Query(sql1,
(DapperTest, imeinvoice) =>
{
DapperTest.imeinvoice = imeinvoice; return DapperTest;
});
var query1 = connection.Execute("update DapperTest set UserName=@UserName
where ID=@ID", new { UserName = "jac1111k", ID=10 });
var query1 = connection.Execute("delete DapperTest where ID=@ID", new { ID=10 });
参考文档:1.https://www.cnblogs.com/qtqq/p/4280245.html (该文档含有封装类)
2.https://www.cnblogs.com/huangxincheng/p/5828470.html
3.https://www.cnblogs.com/huangxincheng/p/5832281.html
4.https://www.cnblogs.com/hy59005271/p/4759623.html