在gridview中循环以发送到存储过程被卡在循环中
本文关键字:循环 存储过程 gridview | 更新日期: 2023-09-27 18:12:45
我想我已经看了很久了。。我有两个数据网格。我想循环遍历它们,并将每个项目发送到一个存储过程。该存储过程会检查重复项,如果存在则返回1,如果不存在则返回0。当我为每个循环做。。我被卡住了。我试着把东西搬来搬去,但运气不好。。这是我的代码:
public int IsExists()
{
foreach (GridViewRow row in gvSerials.Rows)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("usp_InsertReceiptSerials", con))
{
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.Add("@ITEMNMBR", SqlDbType.Char).Value = OpenDescription.SelectedRow.Cells[5].Text.Trim();
cmd.Parameters.Add("@RecLineID", SqlDbType.Int).Value = int.Parse(OpenDescription.SelectedRow.Cells[1].Text.Trim());
cmd.Parameters.Add("@RCPTLNNM", SqlDbType.Int).Value = int.Parse(OpenDescription.SelectedRow.Cells[8].Text.Trim());
cmd.Parameters.Add("@POPRCTNM", SqlDbType.Char).Value = OpenDescription.SelectedRow.Cells[4].Text.Trim();
cmd.Parameters.Add("@SERLTNUM", SqlDbType.Char).Value = row.Cells[0].Text.Trim();
SqlParameter parm = new SqlParameter("@IsExists", SqlDbType.Int);
parm.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parm);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
return IsExists();
if (IsExists() == 1) {
MessageBox.Show("Serials Already Exists!!");
Response.Redirect("Index.aspx"); }
else if (IsExists() == 0) {
MessageBox.Show("Serial Numbers have been updated.", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Response.Redirect("Index.aspx"); }
}
这是存储的过程:
ALTER PROCEDURE [dbo].[usp_InsertReceiptSerials]
@POPRCTNM CHAR(17), @ITEMNMBR CHAR(31), @SERLTNUM CHAR(21), @RecLineID INT, @RCPTLNNM INT, @IsExists INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @RecCount INT
-- DECLARE @IsExists INT
IF EXISTS (SELECT * FROM dbo.vwSerialNumbers WHERE SERLNMBR = Right(@SERLTNUM,20) AND ITEMNMBR = @ITEMNMBR)
BEGIN
SET @IsExists = 1
END
ELSE
BEGIN
INSERT INTO dbo.usr_ReceiptSerials(POPRCTNM, ITEMNMBR, SERLTNUM, FullSerialNumber, EntryDate, RecLineID, RCPTLNNM)
VALUES (@POPRCTNM, @ITEMNMBR, Right(@SERLTNUM,20), @SERLTNUM, GetDate(), @RecLineID, @RCPTLNNM)
SET @RecCount = (SELECT COUNT(*) FROM dbo.usr_ReceiptSerials WHERE RecLineID = @RecLineID)
UPDATE dbo.usr_ReceiptLine SET QTYSHPPD = @RecCount
WHERE RecLineID = @RecLineID
SET @IsExists = 0
END
END
我可以看到一个简单的递归问题,它导致了stackoverflow。
=> return IsExists();
在您的退货处理中,您调用的是相同的函数。如果这不是任何业务逻辑的一部分,请删除此行。
此外,检查if (IsExists() == 1)
用途:
int result = cmd.ExecuteNonQuery();
然后对结果进行评估:
if (result == 1) {
MessageBox.Show("Serials Already Exists!!");
Response.Redirect("Index.aspx"); }
else if (result == 0) {
MessageBox.Show("Serial Numbers have been updated.", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Response.Redirect("Index.aspx"); }
对于生成错误,必须返回方法所需的整数值。例如,它可能是result
本身。
更新:
在您的情况下,您似乎想要读取Output
参数的值。若要从Command对象读取输出参数值,请参阅以下答案。然后result
变量将由Output
参数分配,该参数将在执行命令后从SqlParameter中读取。
基于另一个答案:
public int IsExists()
{
int returnInt =0;
foreach (GridViewRow row in gvSerials.Rows)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("usp_InsertReceiptSerials", con))
{
// /.../
if whatever your testing is true:
returnInt = 1;
or
return returnInt;
or
return 1;
or wait until the end of the method if you want to add more stuff.
}
}
}
// Do any more stuff here.
return returnInt();
// Nothing beyond here is executed.
}
在方法中返回后所做的任何操作都不会被执行。即方法以返回结束,然后将该值带回调用方法。