Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Get Multiple Results from Stored Procedure using Dapper in C#, VB.NET

Feb 18, 2015
Introduction

Here I will explain how to get multiple results or records from stored procedure using dapper in
c#, vb.net or access multiple results or objects from stored procedure in dapper using querymultiple method in c#, vb.net.

Description:
  
In previous articles I explained
start or stop IIS service in c#, vb.net, get values from ienumerable list in c#, vb.net, jQuery upload images without postback in asp.net, wcf tutorial for beginners with example in asp.net and many articles relating to AngularJS, jQuery, asp.net, c# and vb.net. Now I will explain how to get multiple results or records from stored procedure using dapper in c#, vb.net.

To get multiple results from stored procedure using dapper we need to use querymultiple method that would be like as shown

C# Code


using (IDbConnection conn = new SqlConnection(connectionString))
{
IEnumerable<dynamic> results = conn.Query(sql: storedProcedureName,param: parameters,commandType: CommandType.StoredProcedure);
var reader = conn.QueryMultiple(storedProcedureName, parameters, commandType: CommandType.StoredProcedure);
var userdetails = reader.Read<userdetails>().ToList();
var salarydetails = reader.Read<salraydetails>().ToList();
}

public class userdetails
{
public int userid;
public string username;
}
public class salraydetails
{
public int salary;
public int empid;
}

VB.NET Code


Using conn As IDbConnection = New SqlConnection(connectionString)
Dim results As IEnumerable(Of Dynamic) = conn.Query(sql:=storedProcedureName, param:=parameters, commandType:=CommandType.StoredProcedure)
Dim reader = conn.QueryMultiple(storedProcedureName, parameters, commandType:=CommandType.StoredProcedure)
Dim userdetails = reader.Read(Of userdetails)().ToList()
Dim salarydetails = reader.Read(Of salraydetails)().ToList()
End Using

Public Class userdetails
Public userid As Integer
Public username As String
End Class
Public Class salraydetails
Public salary As Integer
Public empid As Integer
End Class


I hope it helps you to get multiple results from stored procedure using dapper…….Happy Coding….

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

5 comments :

Unknown said...

This post is not as like your previous post
First time i did not understand any thing
Please describe in detail with db connectivity and all
And please post its output ,too
Thank you

Unknown said...
This comment has been removed by the author.
Suresh Dasari said...

if you know about dapper ORM then you will understand this post....

SriRam said...

Hi suresh..

I have a question on it. In my scenario, i am getting about 5 result sets..If any of the result set returns null values.. then it throws an error. Please suggest me to proceed. Thanks in advance.

Chief said...

thanks

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.