Multiple SQL statements in one jump using Dapper.NET

ADO.NET has a nice feature that allows you to send multiple SQL statements to the database in one reverse direction and get results for all statements:

var command = new SqlCommand("SELECT count(*) FROM TableA; SELECT count(*) FROM TableB;", connection); using(var reader = command.ExecuteReader()) { reader.Read(); resultA = reader.GetInt32(0); reader.NextResult(); reader.Read(); resultB = reader.GetInt32(0); } 

Is there a similar feature in Dapper.NET?

+51
c # sql sql-server dapper
Oct 12 '13 at 18:03
source share
2 answers

Yes, the Dapper QueryMultiple can do this:

 string query = @"SELECT COUNT(*) FROM TABLEA; SELECT COUNT(*) FROM TABLEB"; using (var multi = connection.QueryMultiple(query, null)) { int countA = multi.Read<int>().Single(); int countB = multi.Read<int>().Single(); } 

According to Mark Gravell, this is an ideal way to fulfill multiple requests in one package.

Note. Dapper creator Sam Saffron has posted a detailed explanation with sample code to use QueryMultiple to accomplish this.

+86
Oct 12 '13 at 18:14
source share
 var grid = connection.QueryMultiple(" SELECT COUNT(*) FROM TABLEA SELECT COUNT(*) FROM TABLEB SELECT COUNT(*) FROM TABLEC"); var lstResult = new List<int>(); var isNext = false; do{ var first2 = info.Read<int>().Single(); lstResult.Add(first2); isNext=info.IsConsumed; } while (!isNext); 
+4
Jun 20 '17 at 8:58
source share



All Articles