使用Dapper查询记录是否存在

分类 : .NET技术
/// <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




分类: .NET技术  发布: 2019-07-10-星期三   访问() .NET技术    Web前端    JAVA开发    语言编程    数据库    电脑知识    代码案例

Copyright © 2017 CSS5.NET教程.CSS5 内容仅用于学习和测试参考。 css5.net All Rights Reserved