欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Dapper查询记录是否存在

程序员文章站 2023-11-03 18:01:58
调用: ......
/// <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