In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces "how to use Dapper" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use Dapper" article can help you solve your doubts.
Dapper supports not only basic CURD and stored procedures, but also operation functions and things.
The code for the dapper operation function is as follows:
Using Dapper;using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks Namespace DapperDemo {public static class OperFunction {/ Dapper calls the function / public static void ExecuteFunction () {/ / connection string string strCon = ConfigurationManager.ConnectionStrings ["DbConnection"] .ConnectionString; / / connection object IDbConnection dbConnection = new SqlConnection (strCon) / / define parameters DynamicParameters paras = new DynamicParameters (); paras.Add ("@ num1", 34); paras.Add ("@ num2", 456); / / return value paras.Add ("@ sum", 0, DbType.Int32, ParameterDirection.ReturnValue); string functionName = "fun_test" / execute int count = dbConnection.Execute (functionName, paras, null, null, CommandType.StoredProcedure); / / get the value of the output parameter int sum = paras.Get ("@ sum"); Console.WriteLine ("sum:" + sum); / / output 390}
The code that dapper supports things is as follows:
Using Dapper;using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks Namespace DapperDemo {public static class OperTransaction {/ Dapper supports things / public static void ExecuteTransaction () {/ / connection string string strCon = ConfigurationManager.ConnectionStrings ["DbConnection"] .ConnectionString; / / connection object IDbConnection dbConnection = new SqlConnection (strCon) String strSQL = "DELETE FROM Student WHERE StudentID=@StudentID"; DynamicParameters paras = new DynamicParameters (); paras.Add ("@ StudentID", 3); / / Open connection before BeginTransaction, otherwise error: invalid operation, connection closed dbConnection.Open (); / / Open thing IDbTransaction transaction = dbConnection.BeginTransaction () Try {dbConnection.Execute (strSQL, paras, transaction: transaction); / / commit transaction transaction.Commit ();} catch (Exception ex) {/ / rollback transaction transaction.Rollback () }
In addition to Transaction, Dapper also supports Transactionscope:
Public static void ExecuteTransactionScope () {using (var transactionscope = new TransactionScope (TransactionScopeOption.Required)) {/ / connection string string strCon = ConfigurationManager.ConnectionStrings ["DbConnection"] .ConnectionString; / / connection object IDbConnection dbConnection = new SqlConnection (strCon); string strSQL = "DELETE FROM Student WHERE StudentID=@StudentID"; DynamicParameters paras = new DynamicParameters () Paras.Add ("@ StudentID", 6); dbConnection.Open (); try {dbConnection.Execute (strSQL, paras); transactionscope.Complete () } catch (Exception ex) {/ / do not commit transactions automatically roll back Console.WriteLine (ex.Message) by default After reading this, the article "how to use Dapper" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow 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.