SqlHelper中IN集合场景下的参数处理(续)

今天早上刷牙时,灵光一现,对于昨天的方案,不需要通过借助or或union的方式来更改sql的in了,即,可以直接生成如下的sql语句:

select * from T_AlipayNotityRecord where trade_status=@trade_status and trade_no in(@no0,@no1,...)

程序代码在上面的基础上稍做处理:

public DataTable GetAlipayNotifyRecords(AlipayPaymentStatus status, params string[] trade_no)
{
    string sql = @"select * from T_AlipayNotityRecord where trade_status=@trade_status and trade_no in({0})";

    List paramList = new List()
{
        new SqlParameter("@trade_status",status.ToString()), 
};
    string sql1 = "";
    for (int i = 0; i < trade_no.Length; i++)
    {
        ** sql1 += ",@no" + i; **
        paramList.Add(new SqlParameter("@no" + i, trade_no[i]));
    }
    sql = string.Format(sql, sql1.Substring(",".Length));

    var ds = SqlHelper.SqlDataSet(ConfigFile.PayCenterConnection, sql, CommandType.Text, paramList.ToArray());
    if (ds == null || ds.Tables.Count == 0)
        return null;
    return ds.Tables[0];
}

这样子生成的sql就很直观了。比上面方案的还简短。

办法总比困难多,这话一点不假!

SqlHelper中IN集合场景下的参数处理(续)_第1张图片
about bug.jpg

你可能感兴趣的:(SqlHelper中IN集合场景下的参数处理(续))