I'm trying to store data from some arrays into the database but only the last index of the arrays were stored. I know I need to fix the looping part but I've no idea how to. Below is the code.
for (int i = 0; i < 5; i++)
{
cmd.Parameters["Mon1"].Value = newTA.MonAry[i];
cmd.Parameters["Tue1"].Value = newTA.TueAry[i];
cmd.Parameters["Wed1"].Value = newTA.WedAry[i];
cmd.Parameters["Thu1"].Value = newTA.ThuAry[i];
cmd.Parameters["Fri1"].Value = newTA.FriAry[i];
}
You need to create and execute the command inside the loop:
for (int i = 0; i < 5; i++)
{
using (var conn = new SqlConnection("Some Connection String"))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "INSERT INTO foo (...) VALUES (...)";
cmd.Parameters["Mon1"].Value = newTA.MonAry[i];
cmd.Parameters["Tue1"].Value = newTA.TueAry[i];
cmd.Parameters["Wed1"].Value = newTA.WedAry[i];
cmd.Parameters["Thu1"].Value = newTA.ThuAry[i];
cmd.Parameters["Fri1"].Value = newTA.FriAry[i];
cmd.ExecuteNonQuery();
}
}
如果您使用的是sqlserver,则可以将此作为表类型参数传递,以便可以插入多个行, 请参见此链接
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.