In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "lightweight ORM framework Dapper application how to return multiple result sets" article knowledge points most people do not understand, so Xiaobian summarized the following content for you, detailed content, clear steps, with a certain reference value, I hope you can read this article to gain something, let's take a look at this article "lightweight ORM framework Dapper application how to return multiple result sets" article bar.
Use Dapper's QueryMultiple method to execute multiple SQL statements at once and return multiple result sets. The code is as follows
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Configuration;using Dapper;using System.Data;using System.Data.SqlClient;using DapperAppQueryMultiple.Model;namespace DapperAppQueryMultiple{ class Program { static void Main(string[] args) { //define connection string string conn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString; using (IDbConnection connection = new SqlConnection(conn)) { var sql = @"SELECT * FROM Users WHERE UserId=@UserId; SELECT * FROM Product WHERE ProductId=@ProductId"; var queryMulti = connection.QueryMultiple(sql, new { UserId = 1, ProductId=3 }); //Note: Read must be obtained in the order of returning the tables above, such as the Users table queried first, so Read must first return the Users table, otherwise no data will be returned. var users = queryMulti.Read(); var products = queryMulti.Read(); Console.WriteLine("**** Product Information ****"); //loop through output products.AsList().ForEach(p => { Console.WriteLine("Product Name:"+p.ProductName+"Price:"+p.Price); }); Console.WriteLine("**** Product Information ****"); Console.WriteLine("**** User Info ****"); users.AsList().ForEach(p => { Console.WriteLine("User Name:"+p.UserName+"Email:"+p.Email+"Address:"+p.Address); }); Console.WriteLine("**** User Info ****"); } Console.ReadKey(); } }}
Program Run Results:
Note: Read must be obtained according to the order of the tables returned above, such as the Users table queried first, so Read must first return the Users table, otherwise no data will be returned. The T generic class in Read does not have to be the same as the database table name. The T generic class in the above code is User, and the database table name is Users.
The following code demonstrates the return order differently:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Configuration;using Dapper;using System.Data;using System.Data.SqlClient;using DapperAppQueryMultiple.Model;namespace DapperAppQueryMultiple{ class Program { static void Main(string[] args) { //Define connection string string conn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString; using (IDbConnection connection = new SqlConnection(conn)) { var sql = @"SELECT * FROM Users WHERE UserId=@UserId; SELECT * FROM Product WHERE ProductId=@ProductId"; var queryMulti = connection.QueryMultiple(sql, new { UserId = 1, ProductId=3 }); //Return query data of Product table first var products = queryMulti.Read(); var users = queryMulti.Read(); Console.WriteLine("**** Product Information ****"); //loop through output products.AsList().ForEach(p => { Console.WriteLine("Product Name:"+p.ProductName+"Price:"+p.Price); }); Console.WriteLine("**** Product Information ****"); Console.WriteLine("**** User Info ****"); users.AsList().ForEach(p => { Console.WriteLine("User Name:"+p.UserName+"Email:"+p.Email+"Address:"+p.Address); }); Console.WriteLine("**** User Info ****"); } Console.ReadKey(); } }}
The above code only modifies the data returned to the table, and the program runs as follows:
The above is about the content of this article on "how to return multiple result sets for lightweight ORM framework Dapper application". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.