【发布时间】:2020-05-10 11:31:19
【问题描述】:
我想为同一个查询输入不同的条目,但我遇到了一个错误:
参数必须是唯一的
有什么办法解决吗?
List<int> hoursList = new List<int>{1,2,3,4,5,6,7};
string connectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
using (var con = new SqlConnection(connectionString))
{
var query = @"INSERT INTO EmployeeTable (EmployeeID, ProjectID, CategoryID, SubCategoryID, Location, Date, Hours)
VALUES (@EmployeeID, @ProjectID, @CategoryID, @SubCategoryID, @Location, @Date, @Hours,)";
using(var cmd = new SqlCommand(query,con))
{
cmd.Parameters.AddWithValue("@EmployeeID",obj.EmployeeID);
cmd.Parameters.AddWithValue("@ProjectID", obj.ProjectID);
cmd.Parameters.AddWithValue("@CategoryID", obj.CategoryID);
cmd.Parameters.AddWithValue("@SubCategoryID", obj.SubCategoryID);
cmd.Parameters.AddWithValue("@Location", obj.Location);
for(int j = 0; j < hoursList.Count; j++)
{
cmd.Parameters.AddWithValue("@Hours", hoursList[j]);
cmd.Parameters.AddWithValue("@Date", DateTime.Now.AddDays(j).ToString("yyyy/MM/dd"));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
【问题讨论】:
标签: c# sql-server ado.net