如何在C#中多次执行存储过程

编程入门 行业动态 更新时间:2024-10-25 10:34:42
本文介绍了如何在C#中多次执行存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个时间表应用程序,用户可以在其中输入/输入一周中不同日期的时间.表单每天处理输入/输出,将它们作为参数填充到存储过程中,并将其添加到数据库中.我将如何最有效地完成此任务?我没有访问数据库的权限,只有存储过程的权限.

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();

如果只有1天的进/出代码,则此代码有效.如果用户填写了多天,我将收到此错误消息:多次提供了参数'@In'.

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.

感谢您的帮助.

推荐答案

SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand cmd = new SqlCommand("insertINOUT", conn); cmd.CommandType = CommandType.StoredProcedure; for (int j = 0; j < weekDays.Length; j++) { **cmd.Parameters.Clear();** cmd.Parameters.Add(new SqlParameter("@UserName", user)); cmd.Parameters.Add(new SqlParameter("@In", in)); cmd.Parameters.Add(new SqlParameter("@Out", out)); cmd.ExecuteReader(); } conn.Close();

(您必须在每次迭代中清除参数.)

(You have to clear the parameters each iteration.)

更多推荐

如何在C#中多次执行存储过程

本文发布于:2023-10-26 09:28:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529766.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!