首頁 > 軟體

輕量級ORM框架Dapper應用支援操作函數和事物

2022-03-08 19:00:31

dapper除了支援基礎的CURD、儲存過程以外,還支援操作函數和事物。

dapper操作函數的程式碼如下:

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
    {
        /// <summary>
        /// Dapper滴呼叫函數
        /// </summary>
        public static void ExecuteFunction()
        {
            // 連線字串
            string strCon = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
            // connection物件
            IDbConnection dbConnection = new SqlConnection(strCon);
            // 定義引數
            DynamicParameters paras = new DynamicParameters();
            paras.Add("@num1", 34);
            paras.Add("@num2", 456);
            // 返回值
            paras.Add("@sum", 0, DbType.Int32, ParameterDirection.ReturnValue);
            string functionName = "fun_test";
            // 執行
            int count = dbConnection.Execute(functionName, paras, null, null, CommandType.StoredProcedure);
            // 獲取輸出引數的值
            int sum = paras.Get<int>("@sum");
            Console.WriteLine("sum:" + sum);//輸出390
        }
    }
}

dapper支援事物的程式碼如下:

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
    {
        /// <summary>
        /// Dapper支援事物
        /// </summary>
        public static void ExecuteTransaction()
        {
            // 連線字串
            string strCon = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
            // connection物件
            IDbConnection dbConnection = new SqlConnection(strCon);
            string strSQL = "DELETE FROM Student WHERE StudentID=@StudentID";
            DynamicParameters paras = new DynamicParameters();
            paras.Add("@StudentID", 3);
            // 在BeginTransaction之前要開啟連線,否則報錯:無效操作,連線已關閉
            dbConnection.Open();
            // 開啟事物
            IDbTransaction transaction = dbConnection.BeginTransaction();
            try
            {
                dbConnection.Execute(strSQL, paras, transaction: transaction);
                // 提交事務
                transaction.Commit();
            }
            catch (Exception ex)
            {
                // 回滾事物
                transaction.Rollback();
            }
        }
    }
}

Dapper除了支援Transaction以外,還支援Transactionscope:

public static void ExecuteTransactionScope()
{
      using (var transactionscope = new TransactionScope(TransactionScopeOption.Required))
      {
            // 連線字串
            string strCon = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
            // connection物件
            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)
            {
                // 不提交事務預設自動回滾
                Console.WriteLine(ex.Message);
            }
      }
}

到此這篇關於Dapper支援操作函數和事物的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com