我正在尝试使用switch case将值插入数据库。在某些情况下,可能需要为特定情况插入多个值。我无法插入对应于案例的两个值。
foreach (int thisColor in allColors)
{
List<Subject> subjectsForThisColor = subjects.Where(x => x.Color == thisColor).ToList();
foreach (Subject s in subjectsForThisColor)
{
test += s.SubjectName + " -" + s.Color + "\n";
switch (s.Color)
{
case 1:
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Monday (first) values (@first)",con);
cmd.Parameters.AddWithValue("first",s.SubjectName);
cmd.ExecuteNonQuery();
con.Close();
break;
case 2:
con.Open();
SqlCommand cmd1 = new SqlCommand("UPDATE Monday set [second] = @second", con);
cmd1.Parameters.AddWithValue("second", s.SubjectName);
cmd1.ExecuteNonQuery();
con.Close();
break;
case 6:
con.Open();
SqlCommand cmd6 = new SqlCommand("UPDATE Monday set [sixth] = @sixth", con);
cmd6.Parameters.AddWithValue("sixth", s.SubjectName);
cmd6.ExecuteNonQuery();
con.Close();
break;
}
}
}
以下是案例值:
C# 1
SSM 2
OOMD 3
MMT 4
Elective-1 5
Elective-2 6
Elective-5 6
Elective-3 7
Elective-4 8
Elective-6 8
因此,当我遇到案例6时,我需要插入值Elective -2
和Elective -5
。现在我只能插入Elective-5.
答案 0 :(得分:0)
让我们稍微清理一下代码,让它更可重复使用:
foreach (int thisColor in allColors)
{
List<Subject> subjectsForThisColor = subjects.Where(x => x.Color == thisColor).ToList();
foreach (Subject s in subjectsForThisColor)
{
test += s.SubjectName + " -" + s.Color + "\n";
switch (s.Color)
{
case 1:
updateOneRow("Monday", "first", s.SubjectName, null);
break;
case 2:
updateOneRow("Monday", "second", s.SubjectName, null);
break;
case 6:
updateOneRow("Monday", "sixth", s.SubjectName, null);
break;
}
}
}
SQL方法
public void updateOneRow(string tableName, string columnName, string value){
con.Open();
query = "SELECT count ("+columnName+") FROM "+tableName;
SqlCommand cmd = New SqlCommand(query, con);
Int32 count = (Int32)cmd.ExecuteScalar();
if(count == 0){
cmd = new SqlCommand("INSERT INTO " + tableName + "("+columnName+") values (@value)"),con);
}
else{
query = "SELECT top 1 "+columnName+" FROM "+tableName;
cmd = New SqlCommand(query, con);
string dbValue = (string)cmd.ExecuteScalar();
if (dbValue != null && dbValue != string.Empty)
value += ";"+dbValue;
cmd = new SqlCommand("UPDATE " + tableName + " set ["+columnName+"] = @value", con);
}
cmd.Parameters.AddWithValue("value",value);
cmd.ExecuteNonQuery();
con.Close();
}
这有助于清理代码,以及为什么要获得Elective-5
,这是因为您在案例6中使用了UPDATE
SQL语句而不是{{1} }}。 Update将覆盖表中的先前值,并替换与where子句匹配的所有当前值,但您没有。因此表中的所有值都将被覆盖。我认为你应该写INSERT
而不是INSERT
[编辑] 我不喜欢它,但更新了代码以满足您的期望。您可能需要调试它,但应该给您一个想法。它检查一行是否已存在,如果不存在则插入。如果是,则检查列是否有值,如果存在,则将其添加到要插入的新值。然后它进行更新。
希望有所帮助!
最终警告 这段代码不是完美的,远非如此,但我希望它能引导您朝着一些稍好的编码实践的正确方向前进。祝好运! 此外,这不是数据库的最佳设计,您应该以不同的方式看待这一点!
[OP&#39代码]
UPDATE
进行必要的更改并执行后,我收到以下错误。
case 6:
con.Open();
string query = "SELECT count ( sixth ) FROM Monday ";
SqlCommand cmd3 = new SqlCommand(query, con);
Int32 count = (Int32)cmd3.ExecuteScalar();
if (count == 0)
{
cmd = new SqlCommand("INSERT INTO Monday (sixth) values (@value)", con);
}
else
{
query = "SELECT top 1 sixth FROM Monday";
cmd = new SqlCommand(query, con);
string dbValue = (string)cmd.ExecuteScalar();
if (dbValue != null && dbValue != string.Empty)
{
value += ";" + dbValue;
//value += s.SubjectName;
//cmd = new SqlCommand("UPDATE Monday set [sixth] = @value", con); <-This is in the wrong place
}
cmd = new SqlCommand("UPDATE Monday set [sixth] = @value", con);//Should be here
}
cmd.Parameters.AddWithValue("value",value);
cmd.ExecuteNonQuery();
con.Close();
break;
答案 1 :(得分:-3)
我会使用带有可选参数的存储过程。