我有一个单列数据表,我想使用 sqlparametercollection 在 SQL where 子句中传递这个数据表。请帮忙。下面是我的代码:
public DataTable getCatsByDepts(DataTable _Depts)
{
SqlConnection conn = new SqlConnection("Server=ax12d;Database=DemoDataAx;Trusted_Connection=True;");
SqlCommand cmd = new SqlCommand("Select Level2 as Category from Mtq_RetailHierarchy Where Level1 IN (@Depts)", conn);
foreach (DataRow row in _Depts.Rows)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("Department", row.Field<string>("Department"));
//cmd.ExecuteNonQuery();
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable SelectedCatsData = new DataTable();
da.Fill(SelectedCatsData);
return SelectedCatsData;
}`