使用Dapper查询记录是否存在

/// <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

 

上一篇:开心乐园源码-PHP实现把MySQL数据库导出为.sql文件实例


下一篇:mysql 每个月创建新表