dapper

Simple but good example on how to use Dapper with Structuremap and dependency injection

六眼飞鱼酱① 提交于 2019-12-04 22:48:28
问题 I am trying to understand how to use Dependency Injection with Dapper (IDbConnection) and still being able to use built in dispose. I have found a couple of articles on the web but non that I think is easy to understand. What I am trying to figure out is how to make this simple class be testable: public class UserProfileRepository : IUserProfileRepository { private readonly IConfigRepository _configRepository; public UserProfileRepository(IConfigRepository configRepository) {

How can I get Dapper to map .net datetime to datetime2?

若如初见. 提交于 2019-12-04 22:36:06
Pretty simple, I'm converting our existing system from EF to Dapper. For various corporate reasons we can't really change the database, some of the tables have columns that are of type DateTime2. Dapper converts any .net DateTime to DbType.DateTime. Someone must have bumped against this before and found an easy solution ? Dapper is litterally a single file that you include into your code base. Just edit the file: Replace (around line 300): typeMap[typeof(Guid)] = DbType.Guid; typeMap[typeof(DateTime)] = DbType.DateTime; typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset; typeMap[typeof

销帮帮数据处理工具

蹲街弑〆低调 提交于 2019-12-04 20:26:29
开发背景: 公司CRM采购了销帮帮的CRM系统,由于CRM系统不完善,导出功能不能满足公司对数据进行分析的需求。每次整理数据,分析人员部门等各种情况,再有如果人员重名,销帮帮不能区分出具体是谁,必须去根据人员或其他数据进行区分。 解决方案: 由于销帮帮数据的人员是有UserID的,而该UserID对应钉钉的UserID,所以可以根据钉钉提供的API接口轻松的判断出人员部门、分公司等信息,不用关心人员重名的情况。 开发环境: 软件使用C#+SQLSERVER进行开发。 使用教程: 开始前先给大家看看软件的整体界面。 软件主要包括清空今日数据,采集、数据分析、同步用户信息、获取数据 5部分功能。 创建并配置SQLServer数据库 在安装好的SQLServer服务器上,创建数据库,数据库名称根据需要定义,此处我定义的数据库名称是xbb,如下图的配置[1],正确配置数据库连接 获取销帮帮的组织编码和Token 根据销帮帮提供的网址[https://dingtalk.xbongbong.com/apiSetting/detail.html]获取对应的组织编码和token.,如下图配置[2]配置销帮帮石药使用的组织编码和Token. 创建企业内部应用 在钉钉的【开发者后台】创建企业内部应用。开放查询部门、人员信息的权限即可。并配置对应的appkey/appsecret到下图【3】处。

Does Dapper support SQL 2008 Table-Valued Parameters 2?

落爺英雄遲暮 提交于 2019-12-04 19:34:49
问题 I know that dapper can support TVF, but how do you send extra parameters along with TVF (without adding it to the IntDynamicParam class)? See the below example from Tests.cs, i have modified to add the extra parameter: connection.Execute("CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)"); connection.Execute("CREATE PROC get_ints @x int, @ints int_list_type READONLY AS select * from @ints"); I tried the following but got errors (No mapping exists from object type SqlMapper

Dapper with .NET Core - injected SqlConnection lifetime/scope

情到浓时终转凉″ 提交于 2019-12-04 19:11:24
问题 I'm using .NET Core Dependency Injection to instantiate a SqlConnection object during the application startup, which I'm then planning to inject in my repository. This SqlConnection will be used by Dapper to read/write data from the database within my repository implementation. I am going to use async calls with Dapper. The question is: should I inject the SqlConnection as transient or as a singleton? Considering the fact that I want to use async my thought would be to use transient unless

Is AsList() better than ToList() with IDbConnection.Query() which returns IEnumerable?

主宰稳场 提交于 2019-12-04 18:27:19
问题 I read this answer from Marc Gravell (@MarcGravell): https://stackoverflow.com/a/47790712/5779732 The last line says: As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy. That statement is about QueryMultiple() which returns GridReader . In my understanding, System.Linq provides an extension method IEnumerable.ToList() . Following is from Microsoft about ToList() . The ToList(IEnumerable) method forces immediate query evaluation and returns a List that

使用dapper遇到的问题及解决方法

别说谁变了你拦得住时间么 提交于 2019-12-04 17:59:17
在使用dapper进行数据查询时遇到的一个问题,今天进行问题重现做一个记录,免得忘记以后又犯同样的错误。 自己要实现的是:select * from tablename where id in(1,2)这样的一个查询语句。自己以为的写法应该是这样的,代码如下: List<CICUser> userList = new List<CICUser>(); using (IDbConnection conn = new SqlConnection(sqlConnectionString)) { int[] idarr = new int[] { 1, 2, 3 }; string sqlCommandText = @"SELECT * FROM CICUser s WHERE s.UserId IN (@UserId) "; userList = conn.Query<CICUser>(sqlCommandText, new { UserId = idarr },null,true,null, CommandType.Text).ToList(); } 运行之后报错,如下: 找问题原因是就是觉得自己写的没有错,那到底是哪里出了问题呐,又不想去研究源码,那就找看咋个拿到生成的sql语句,比对下sql语句,看哪里有问题。 接下来就根据数据库的 工具,进行跟踪得到sql语句,如下: exec

Why does Dapper QueryAsync<T> behave differently from Query<T> with sproc return values?

前提是你 提交于 2019-12-04 17:27:08
I have a stored procedure that performs a check, and returns either a row or a non-zero return value: CREATE PROCEDURE dbo.ConditionalGet @ID INT AS BEGIN -- this is a silly made up condition just to test the issue IF @ID % 2 = 1 BEGIN RETURN -1 END SELECT * FROM MyTable WHERE ID = @ID RETURN 0 END And I have a C# repo class that uses Dapper to return the result or throw an exception: public class Repo { public MyClass Get(int id) { using (var conn = GetSqlConnection()) { var p = new { ID = id }; var pWithReturnValue = new DynamicParameters(p); p.Add("return", null, DbType.Int32,

Which transaction is better with Dapper: BEGIN TRAN or TransactionScope?

眉间皱痕 提交于 2019-12-04 15:33:25
I've just started using Dapper and I was wondering which pattern for transactions would be better. When I write SQL I prefer to use the transaction already in the script with: BEGIN TRAN -- insert, update etc. COMMIT because it's easier to test it but there is also the question transaction with dapper dot net where .net transactions are used so now I'm not sure which one I should actually use. Does either method have any dis/advantages over the other? Actually, this has nothing to do with Dapper. Refer this answer. TransactionScope or connection.BeginTransaction or "Transaction in stored

Why does Dapper's .Execute(…) return an int?

孤街醉人 提交于 2019-12-04 14:57:45
问题 Anyone know why Dapper returns an int from .Execute(...) ? I can't find this documented anywhere. 回答1: The integer represents the number of rows that were affected by your query. It returns an integer so you know if your query has worked. If zero is returned and you expected something to have changed then you know there is a problem. 回答2: Because DbCommand.ExecuteNonQuery (which Dapper uses internally, no doubt) returns an int for the number of rows affected. Why? Because it's more or less