調用: ...
/// <summary> /// Dapper數據訪問抽象基礎類 /// </summary> public class DapperHelper { public static string connectionString = DESEncrypt.Decrypt(ConfigurationManager.ConnectionStrings["SOACon"].ConnectionString); /// <summary> /// 連接資料庫 /// </summary> /// <returns></returns> public static IDbConnection DbConnection() { var connection = new SqlConnection(connectionString);//連接SQL Server資料庫 connection.Open(); return connection; } /// <summary> /// 執行是否存在數據 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sqlStr">查詢(例:SELECT COUNT(1) FROM XXX )</param> /// <param name="param">匿名類型</param> /// <returns></returns> public static bool Exists(string sqlStr, Object param) { using (IDbConnection conn = DapperHelper.DbConnection()) { int count = conn.Query<int>(sqlStr, param).FirstOrDefault(); return count > 0 ? true : false; } } }
調用:
#region 判斷是否存在庫存(不論熱前還是熱後) /// <summary> /// 判斷是否存在庫存(不論熱前還是熱後) /// </summary> /// <param name="processCardNumber">流轉卡號</param> /// <returns></returns> public bool ExistInventory(string processCardNumber) { string sqlStr = " SELECT COUNT(1) FROM udtWip_Inventory WHERE processCardNumber=@processCardNumber "; return DapperHelper.Exists(sqlStr, new { processCardNumber = processCardNumber }); } #endregion