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

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支持操作函數和事物的文章就介紹到這瞭。希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: