循环内执行非查询
我正在尝试在 C# 循环中插入数据库记录。
当我像这样对值进行硬编码时,它会起作用:
string query3 = "INSERT INTO furniture (room_id,member_id) VALUES (222,333);";
SqlCommand cmd3 = new SqlCommand(query3, sqlConnection3);
sqlConnection3.Open();
for (int i = 0; i < arrItemsPlanner.Length; i++)
{
try
{
cmd3.ExecuteNonQuery();
}
catch
{
return "Error: Item could not be saved";
}
finally
{
//Fail
}
}
但是当我使用参数化查询时,它不起作用 - 即使我像这样将值硬编码到参数化查询中:
string query3 = "INSERT INTO furniture (room_id,member_id) VALUES (@room_id,333);";
SqlCommand cmd3 = new SqlCommand(query3, sqlConnection3);
sqlConnection3.Open();
for (int i = 0; i < arrItemsPlanner.Length; i++)
{
try
{
cmd3.Parameters.Add("@room_id", System.Data.SqlDbType.Int);
cmd3.Parameters["@room_id"].Value = 222;
cmd3.ExecuteNonQuery();
}
catch
{
return "Error: Item could not be saved";
}
finally
{
//Fail
}
}
任何人都可以看到我在这里出错的地方吗?
非常感谢!
I'm trying to insert a database record inside a loop in C#.
It works when I hard code the values like this:
string query3 = "INSERT INTO furniture (room_id,member_id) VALUES (222,333);";
SqlCommand cmd3 = new SqlCommand(query3, sqlConnection3);
sqlConnection3.Open();
for (int i = 0; i < arrItemsPlanner.Length; i++)
{
try
{
cmd3.ExecuteNonQuery();
}
catch
{
return "Error: Item could not be saved";
}
finally
{
//Fail
}
}
But when I use parameterised queries it doesn't work - even if I hard code a value into the parameterised query like this:
string query3 = "INSERT INTO furniture (room_id,member_id) VALUES (@room_id,333);";
SqlCommand cmd3 = new SqlCommand(query3, sqlConnection3);
sqlConnection3.Open();
for (int i = 0; i < arrItemsPlanner.Length; i++)
{
try
{
cmd3.Parameters.Add("@room_id", System.Data.SqlDbType.Int);
cmd3.Parameters["@room_id"].Value = 222;
cmd3.ExecuteNonQuery();
}
catch
{
return "Error: Item could not be saved";
}
finally
{
//Fail
}
}
Can anyone see where I'm going wrong here?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看起来您正在一遍又一遍地添加到命令的参数集合中。每次迭代时清除它。
我还建议抛出实际的异常,以便您可以看到问题所在。
It looks like you are adding to the command's parameter collection over and over. Clear it with each iteration.
I would also suggest throwing the actual exception so you can see what the problem is.
经测试&简单的解决方案。
如果在循环中使用参数,则需要在执行查询后清除参数。
所以你可以用它
Tested & simple solution.
If you are using parameters in loop You need to clear the parameters after execution of query.
So you can use that
这未经测试,但应该可以作为替代方案。只需添加一次并不断更新其值。
顺便说一句,您的
SqlCommand
应该位于using
块以及您的SqlConnection
内。未显示完整的代码,因此我不知道您的连接是否实际上是以这种方式完成的。This is untested but should work as an alternative. Just add it once and continually update its value.
As an aside, your
SqlCommand
should be within ausing
block as well as yourSqlConnection
. The full code isn't shown so I don't know if your connection is actually done in such as way.您正在做的是在循环迭代中添加一个参数。在下面的代码中,它添加了一次参数,并且只修改了单个参数的值。试试这个:
What you are doing is adding a parameter ever loop iteration. In the below code, it adds the parameter once, and just modifies the single parameter's value. Try this:
是的,不要在循环中添加参数,仅设置其值:
Yes, don't add the parameter in the loop, only set its value:
// 使用sql server时,不要在循环中继续添加它
在参数集合中没有@needed
// don't keep adding it in the loop either
No @needed in the parameters collection whne using sql server
对于那些正在查看此线程的人来说,这是另一个解决方案。创建两个连接。一个用于循环,另一个用于发送 NonQuery 语句。这对我有用。
Another solution for those who are looking at this thread. Create two connections. One for your loop and another to send your NonQuery statements. This worked for me.