如何在C#中多次执行存储过程
我有一个时间表应用程序,用户可以在其中输入一周中不同日期的进出时间。该表单处理每天的输入/输出,将它们作为参数填充到存储过程中并将它们添加到数据库中。我怎样才能最有效地完成这个任务?我无权访问数据库,只能访问存储过程。
这是后面的裸代码,我删除了一些不必要的代码。
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("insertINOUT", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName", user));
for (int j = 0; j < weekDays.Length; j++)
{
cmd.Parameters.Add(new SqlParameter("@In", in));
cmd.Parameters.Add(new SqlParameter("@Out", out));
cmd.ExecuteReader();
}
conn.Close();
如果只有 1 天的进出时间,则该代码有效。如果用户填写多天,我会收到此错误:多次提供参数“@In”。
感谢您的帮助。
I have a time sheet app where users enter their time in/out for different days of the week. The form processes the in/out from each day, stuff them as parameters into a stored procedure and add them to the database. How would I accomplish this most efficiently? I don't have access to the DB, just the stored procedures.
This is the bare code behind, I've stripped out some unnecessary codes.
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("insertINOUT", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName", user));
for (int j = 0; j < weekDays.Length; j++)
{
cmd.Parameters.Add(new SqlParameter("@In", in));
cmd.Parameters.Add(new SqlParameter("@Out", out));
cmd.ExecuteReader();
}
conn.Close();
The code works if there's only 1 day of in/out. If the users fill out multiple days, I'll get this error: Parameter '@In' was supplied multiple times.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
(每次迭代都必须清除参数。)
(You have to clear the parameters each iteration.)
另一种选择是,您可以更改 SqlCommand 的范围,以便每次都重新创建它。
似乎有点浪费,但有一些库以这种方式工作(我想到了企业库 DAAB)。
Another alternative, you could change the scope of the SqlCommand so that it is recreated each time.
Seems a bit wasteful, but there are some libraries that work this way (the Enterprise Library DAAB comes to mind).
这是因为您试图将相同的参数重新添加到同一个 sqlcommand 对象。为了获得最佳性能,在启动 for 循环之前,打开连接并添加不带值的参数。然后,在 for 循环内部,您所做的就是设置参数的值,然后执行该过程。无需在循环的每次迭代中重新创建参数本身,您只是在浪费资源。试试这个:
我知道这个问题被问到已经有好几年了,但我想仍然会有人搜索这个问题,也许会发现这个答案很有帮助。
It is because you were trying to re-add the same parameters to the same sqlcommand object. For best performance, BEFORE you start your for loop, open your connection and add the parameters without values. Then, INSIDE your for loop all you are doing is setting the value of the parameters and then executing the procedure. No need to recreate the parameters themselves each iteration of the loop, you're just burning resources for nothing. Try this:
I know it's been some years since this was asked, but I figured there will still be people searching on this and maybe will find this answer helpful.
您收到该错误的原因是因为 for 循环多次重新添加参数:
执行此操作的正确方法是清除 foo 循环最后一行的参数集合,或者简单地检查参数是否已存在并设置其值而不是执行
Parameters.Add
The reason you are getting that error is because the for loop is re-adding the parameter multiple times:
The proper way to do this is either clear the Parameters collection at the last line of the foor loop or simply check if the parameter already exists and set its value instead of doing
Parameters.Add