C# 如何简化mysql中的更新查询?
我正在做一个大学出勤项目(Winforms、Mysql、C#) 我想在所有期间列中将0更新为p,1更新为Ab,2更新为OD。为此,我编写了一个For循环和三个update查询 在一个查询中执行此操作是否有可能 请帮帮我C# 如何简化mysql中的更新查询?,c#,mysql,C#,Mysql,我正在做一个大学出勤项目(Winforms、Mysql、C#) 我想在所有期间列中将0更新为p,1更新为Ab,2更新为OD。为此,我编写了一个For循环和三个update查询 在一个查询中执行此操作是否有可能 请帮帮我 for (int i = 1; i <= 8; i++) { string period = "Period" + i; string t1p = period1;
for (int i = 1; i <= 8; i++)
{
string period = "Period" + i;
string t1p = period1;
command.CommandText = "update attendance_daily_rpt set " + t1p + " = 'P' where " + t1p + " = 0";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
command.CommandText = "update attendance_daily_rpt set " + t1p + " = 'Ab' where " + t1p + " = 1";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
command.CommandText = "update attendance_daily_rpt set " + t1p + " = 'OD' where " + t1p + " = 2";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
for(inti=1;iSQL:
让自己用C#string;)你可以使用语句
UPDATE
attendance_daily_rpt
SET
PeriodX = CASE PeriodX
WHEN '0' THEN 'P'
WHEN '1' THEN 'Ab'
WHEN '2' THEN 'OD'
ELSE PeriodX
END
可能,但它很可能是一个怪物,它的性能可能比24个单独的目标更新差得多。我认为您当前的解决方案没有什么特别的问题,除了以下事实:
- 您可能希望将其作为一项交易进行;及
- 可能不需要为每个SQL语句关闭和打开连接
我会尝试从以下内容开始:
connection.Open();
// Start transaction with whatever it takes.
for (int i = 1; i <= 8; i++) {
string t1p = "Period" + i;;
command.CommandText = "update attendance_daily_rpt set " + t1p
+ " = 'P' where " + t1p + " = '0'";
command.ExecuteNonQuery();
command.CommandText = "update attendance_daily_rpt set " + t1p
+ " = 'Ab' where " + t1p + " = '1'";
command.ExecuteNonQuery();
command.CommandText = "update attendance_daily_rpt set " + t1p
+ " = 'OD' where " + t1p + " = '2'";
command.ExecuteNonQuery();
}
// Commit transaction.
connection.Close();
string[] vals = new string[] {"P", "Ab", "OD"};
connection.Open();
// Start transaction with whatever it takes.
for (int i = 1; i <= 8; i++) {
string t1p = "Period" + i;;
for (int j = 0; j < vals.Length; j++) {
command.CommandText = "update attendance_daily_rpt set " + t1p
+ " = '" + vals[j] +"' where " + t1p + " = '" + j + "'";
command.ExecuteNonQuery();
}
}
// Commit transaction.
connection.Close();
connection.Open();
//用一切代价开始交易。
对于(int i=1;i创建一个用于维护映射的表(考勤\每日\定期\映射):
我用您的想法更新,…command.CommandText=“update Attentication\u daily\u rpt set”+t1p+“=大小写为“0”时,大小写为“P”,大小写为“1”,大小写为“AB”,大小写为“2”,大小写为“OD”,大小写为“结束”;connection.Open();command.ExecuteNonQuery();connection.Close();但是0,1,2中的所有列仅更新为'AB'。我在这个查询中犯了什么错误。尝试将大小写更改为大小写t1pI无法获得sll。我不知道mysql中的这个大小写概念。我最好阅读语法。谢谢。如果我在这里得到注释,谢谢sll。现在我得到了答案。再次感谢支持。因此,这个方法减少了连接打开和close.是吗?.类型转换如何?您将大小写PeriodX设置为数字,但将字符串指定为列名?然后在字符串连接中用列名替换“t1p”。ya t1p是列名。bzs Period1到Period8是表中的列。
string[] vals = new string[] {"P", "Ab", "OD"};
connection.Open();
// Start transaction with whatever it takes.
for (int i = 1; i <= 8; i++) {
string t1p = "Period" + i;;
for (int j = 0; j < vals.Length; j++) {
command.CommandText = "update attendance_daily_rpt set " + t1p
+ " = '" + vals[j] +"' where " + t1p + " = '" + j + "'";
command.ExecuteNonQuery();
}
}
// Commit transaction.
connection.Close();
+-----------+---------+
| FromValue | ToValue |
+-----------+---------+
| 0 | P |
| 1 | Ab |
| 2 | OD |
+-----------+---------+
UPDATE
attendance_daily_rpt
INNER JOIN attendance_daily_rpt_map ON attendance_daily_rpt.t1p = attendance_daily_rpt_map.FromValue
SET
attendance_daily_rpt.t1p = attendance_daily_rpt_map.ToValue