Dapper 条件语句(Where) 中参数使用

public static List<ECInput> GetECInputList(DateTime beginDate,DateTime endDate,string[] barcodes,string[] paraTypes)
        {
            List<ECInput> result = null;
            
            using (var conn = new System.Data.SqlClient.SqlConnection(ConnString))
            {   
                conn.Open();

                var sql = "select * from EC_Input where InputTime >= @BeginDate and InputTime < @EndDate ";

                var dynamicParams = new DynamicParameters();

                dynamicParams.Add("BeginDate", beginDate.Date);
                dynamicParams.Add("EndDate", endDate.Date.AddDays(1));


                if (barcodes != null && barcodes.Length > 0)
                {   
                    sql += " and Barcode in @Barcodes";
                    dynamicParams.Add("Barcodes", barcodes);
                }

                if (paraTypes != null && paraTypes.Length > 0)
                {
                    sql += " and ParaType in @ParaTypes";
                    dynamicParams.Add("ParaTypes", paraTypes);
                }

                var data = conn.Query<ECInput>(sql, dynamicParams);

                conn.Close();

                result = data.ToList();
            }

            return result;
        }

 

你可能感兴趣的:(Dapper 条件语句(Where) 中参数使用)