我正在尝试使用 foreach 控件循环遍历字符串数组,然后使用每个值在数据库中插入信息。有人可以帮我理解为什么在 using 子句中我不能使用 foreach 变量吗?
string[] ship_ids = ShipsInScope.Split('|');
foreach (string ship_id in ship_ids)
{
using (SqlCommand InsertCommand = new SqlCommand("insert into PROJECT_SHIP (CR_Number, Ship_Id) VALUES (@CR_Number, @CCF_Number)", DBConn))
{
InsertCommand.Parameters.Add("@CR_Number", SqlDbType.NVarChar, 10).Value = CRNumber;
InsertCommand.Parameters.Add("@Ship_Id", SqlDbType.NVarChar, 10).Value = Ship_Id;
InsertCommand.ExecuteNonQuery();
InsertCommand.Dispose();
}
}