运行多个SqlCommand和ExecuteNonQuery
本文关键字:ExecuteNonQuery SqlCommand 运行 | 更新日期: 2023-09-27 18:25:39
我试图在同一连接中运行多个SqlCommand,但由于某种原因,程序将在第二个处停止
command.ExecuteNonQuery();
这是我的代码:
string queryString = "SELECT DISTINCT Titre from infosHoraire where Salle='DOO';" +
"SELECT DISTINCT Titre from infosHoraire where Salle='FOO' and Jour <='" + finDate + "';" +
"SELECT DISTINCT Titre from infosHoraire where Salle='GOO' and Jour <='" + finDate + "';";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleXMLtoDB"].ConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
// on remplit le dico
while (reader.Read())
{
MoviesList.Add(reader[0].ToString(), "0");
searchCode(reader[0].ToString(), MoviesList);
//Console.WriteLine(" On rajoute le code {0}", MoviesList[reader[0].ToString()]);
}
reader.NextResult();
while (reader.Read())
{
if (!MoviesList.ContainsKey(reader[0].ToString()))
{
MoviesList.Add(reader[0].ToString(), "0");
searchCode(reader[0].ToString(), MoviesList);
}
}
reader.NextResult();
while (reader.Read())
{
if (!MoviesList.ContainsKey(reader[0].ToString()))
{
MoviesList.Add(reader[0].ToString(), "0");
}
}
foreach (string key in MoviesList.Keys)
{
Console.WriteLine("MoviesList {0}, code {1} .", key, MoviesList[key]);
// RAJOUTER DONNEES HORAIRES
command.CommandText = "INSERT INTO infosHoraire (Code) VALUES ('" + MoviesList[key] + "') where Titre = '" + key + "'";
//cmd.Parameters.AddWithValue("@code", MoviesList[key]);
IT STOPS HERE.
command.ExecuteNonQuery();
}
}
catch (Exception)
{
//Console.WriteLine("{0} Exception caught.", e);
}
finally
{
// Always call Close when done reading.
reader.Close();
connection.Close();
}
}
MoviesList是一个
Dictionary<string, string>
我真的找不到问题的根源。
如果我删除这个,它会很好用:
foreach (string key in MoviesList.Keys)
{
//literaltest.Text += "<br/> dictionnaire " + key + "," + MoviesList[key];
Console.WriteLine("MoviesList {0}, code {1} .", key, MoviesList[key]);
// RAJOUTER DONNEES HORAIRES
command.CommandText = "INSERT INTO infosHoraire (Code) VALUES ('" + MoviesList[key] + "') where Titre = '" + key + "'";
//cmd.Parameters.AddWithValue("@code", MoviesList[key]);
command.ExecuteNonQuery();
}
编辑:试着在捕获上设置一个断点,看看是否有错误。
catch (Exception e)
{
//Console.WriteLine("{0} Exception caught.", e);
}
将鼠标悬停在e上,您应该能够看到错误消息。然后把它贴在这里。可能是您的查询导致了问题,而不是命令对象
编辑。读取器对象仍在运行,这限制了您再次使用命令对象。在尝试执行下一个命令之前,请先关闭读卡器
reader.Close();
从以下insert
查询中删除where
条件
command.CommandText = "INSERT INTO infosHoraire (Code) VALUES ('" + MoviesList[key] + "') where Titre = '" + key + "'";
用于读取记录的SQLDataReader仍处于打开状态并使用该连接。在同一连接上执行另一个命令之前,您需要关闭该读卡器。例如
reader.Close();
foreach (string key in MoviesList.Keys)
{
Console.WriteLine("MoviesList {0}, code {1} .", key, MoviesList[key]);
// RAJOUTER DONNEES HORAIRES
command.CommandText = "INSERT INTO infosHoraire (Code) VALUES ('" + MoviesList[key] + "') where Titre = '" + key + "'";
//cmd.Parameters.AddWithValue("@code", MoviesList[key]);
IT STOPS HERE.
command.ExecuteNonQuery();
}
querystring= "INSERT INTO infosHoraire (Code) VALUES ('" + MoviesList[key] + "')";
SqlCommand(命令)仍然很忙-您不能将其用于insert语句。您需要使用新的SqlCommand打开一个新的SqlConnection。尽管如果您只是使用块将foreach移动到原始查询之外,可能会更干净,因为此时您只引用内存中的字典。
如前所述,插入语句中还有一个where子句,它没有意义。。。你的意思是更新声明吗?
放下空捕获,你会看到一些异常消息,这会让你的生活变得更轻松。
//编辑您的选择语句,试试这个
SELECT DISTINCT Titre from infosHoraire where Salle in ('DOO','FOO','GOO') and Jour <='" + finDate + "'";