Passing a SQL parameter to an IN() clause using typed datasets in .NET

后端 未结 8 2040
别跟我提以往
别跟我提以往 2020-11-28 10:31

First apologies as there are similar questions on this site, but none of them answer this problem directly.

Im using typed datasets in VS 2010. I create a TableAdapt

8条回答
  •  一向
    一向 (楼主)
    2020-11-28 11:16

    You also can create a list of IDs parameters so instead of using @IDs you will use @ID1, @ID2, @ID3, etc

    var sql = "SELECT * from Table WHERE ID IN (" + getKeys(values.Count) + ")";
    

    And getKeys(count) do something like this:

    var result = string.Empty;
                for (int i = 0; i < count; i++)
                {
                    result += ", @ID" + i;
                }
                return string.IsNullOrEmpty(result) ? string.Empty : result.Substring(1);
    

    and Finally, add the parameters:

    foreach (int i = 0; i < values.Count; i++)
                {
                    cmd.Parameters.Add(new SqlParameter("@ID" + i, SqlDbType.VarChar) { Value = values[i]});
                }
    

提交回复
热议问题