我在C#中执行两个查询时遇到问题。当我尝试执行第二个时,它会给我以下消息:
列'FIRST_NAME'不能为空
...这是指我之前成功执行的第一个查询。我想第二个查询仍然有这个列名,即使我没有在这里使用它。
正如您所看到的,我创建了一个新的StringBuilder对象并清除了第二个查询的参数,但不知怎的,我仍然收到此消息。
// Executed first query. Now moving onto second query without calling .Close()
// Store the last inserted id as the child's local id
child.ID = cmd.LastInsertedId;
// Store the child id into the parent record
query = new StringBuilder();
cmd.Parameters.Clear();
query.Append("UPDATE players SET child_id = @child_id WHERE player_id = @player_id;");
cmd.Parameters.AddWithValue("child_id", child.ID);
cmd.Parameters.AddWithValue("player_id", child.ParentID);
// Execute query
rows_affected = cmd.ExecuteNonQuery();
conn.Close();
答案 0 :(得分:1)
您似乎没有将CommandText
属性设置为新查询。您仍在使用第二个参数执行第一个查询。
// Executed first query. Now moving onto second query without calling .Close()
// Store the last inserted id as the child's local id
child.ID = cmd.LastInsertedId;
// Store the child id into the parent record
query = new StringBuilder();
cmd.Parameters.Clear();
query.Append("UPDATE players SET child_id = @child_id WHERE player_id = @player_id;");
//*** ADD THIS LINE HERE ***//
cmd.CommandText = query.ToString();
cmd.Parameters.AddWithValue("child_id", child.ID);
cmd.Parameters.AddWithValue("player_id", child.ParentID);
// Execute query
rows_affected = cmd.ExecuteNonQuery();
conn.Close();