将字符串数组添加到SQL查询

时间:2017-09-12 19:41:07

标签: c# sql sqlcommand

我有一个由标识符组成的字符串数组。我想使用这些标识符从SQL获取一些值。有没有办法将字符串值添加到SqlCommand个参数?

我想创建一个类似的查询:

select CaseList from MasterReportData where Id = 1 OR Id = 2 OR Id = 3

这是我的C#代码:

public static List<string> GetCaseList(string[] masterIdList)
    {

        try
        {
            string query = "  select CaseList from MasterReportData where @masterId";
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.AddWithValue("masterId", ***);
            conn.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    list.Add(reader[0].ToString());
                }
            }
            conn.Close();
        }
        catch (Exception e)
        {
            var err= 0;
        }
        return list;
    }

3 个答案:

答案 0 :(得分:1)

有很多不同的方法可以做到这一点,但我更喜欢创建一个可能值的临时表。这样你可以做类似

的事情
select CaseList from MasterReportData where Id IN(select Id from tempTable)

答案 1 :(得分:0)

最好的方法(使用sql优化)将是:

创建您的类型:

CREATE TYPE dbo.IntTTV AS TABLE  
( Id int ) 

你的ID:

var ids = new List<int>
{
  1,
  2,
  3,
}

创建架构:

var tableSchema = new List<SqlMetaData>(1)
{
  new SqlMetaData("Id", SqlDbType.Int) // I think it's Int
}.ToArray();

在C#中创建表格

var table = ids
  .Select(i => 
  {
    var row = new SqlDataRecord(tableSchema);
    row.SetInt32(0, i);
    return row;
  })
  .ToList();

创建SQL参数

var parameter = new SqlParameter();
parameter.SqlDbType = SqlDbType.Structured;
parameter.ParameterName = "@Ids";
parameter.Value = table;
parameter.TypeName = "dbo.IntTTV";

var parameters = new SqlParameter[1]
{
  parameter
};

略微更改您的查询(这只是一个示例:)

string query = "select mrd.CaseList from MasterReportData mrd"
  + " inner join @ids i on mrd.Id = i.id";

答案 2 :(得分:-1)

    public static List<string> GetCaseList(string[] masterIdList)
    {
        List<string> list = new List<string>();
        try
        {
            string query = "select CaseList from MasterReportData where ";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                int i = 0;
                SqlCommand cmd = new SqlCommand(query, conn);
                for(i = 0; i < masterIdList.Length; i++)
                {
                    var parm = "@ID" + i;
                    cmd.Parameters.Add(new SqlParameter(parm, masterIdList[i]));
                    query += (i > 0 ? " OR " : "") + " Id = " + parm;
                }
                cmd.CommandText = query;
                //cmd.Parameters.AddWithValue("masterId", ***);
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(reader[0].ToString());
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.ToString();
        }
        return list;
    }