将字符串数组添加到 SQL 查询
Add string array to SQL query
我有一个由标识符组成的字符串数组。我想使用这些标识符从 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;
}
您可以通过多种不同的方式来执行此操作,但我更喜欢创建可能值的临时 table。这样你就可以做类似
select CaseList from MasterReportData where Id IN(select Id from tempTable)
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;
}
最佳方法(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# 中创建 table
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";
我有一个由标识符组成的字符串数组。我想使用这些标识符从 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;
}
您可以通过多种不同的方式来执行此操作,但我更喜欢创建可能值的临时 table。这样你就可以做类似
select CaseList from MasterReportData where Id IN(select Id from tempTable)
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;
}
最佳方法(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# 中创建 table
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";