C# 通过gridview循环以发送到存储过程被卡在循环中
我想我已经看这个很久了。。我有两个包含数据的GridView。我想循环遍历它们,并将每个项发送到存储过程。该存储过程检查重复项,如果存在则返回1,如果不存在则返回0。好吧,当我做for-each循环时。。我被卡住了。我试过在没有运气的情况下移动东西。。这是我的密码:C# 通过gridview循环以发送到存储过程被卡在循环中,c#,stored-procedures,datagridview,foreach,C#,Stored Procedures,Datagridview,Foreach,我想我已经看这个很久了。。我有两个包含数据的GridView。我想循环遍历它们,并将每个项发送到存储过程。该存储过程检查重复项,如果存在则返回1,如果不存在则返回0。好吧,当我做for-each循环时。。我被卡住了。我试过在没有运气的情况下移动东西。。这是我的密码: public int IsExists() { foreach (GridViewRow row in gvSerials.Rows) { using (SqlConnection con = n
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
我可以看到一个导致堆栈溢出的简单递归问题
=> return IsExists();
在returns语句中,调用的是同一个函数。如果这不是任何业务逻辑的一部分,请删除该行
另外,要检查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"); }
对于生成错误,必须返回方法所需的整数值。
例如,它可能是结果
本身
更新:
在您的情况下,似乎需要读取输出
参数的值。要从命令对象读取输出参数值,请参见。然后,结果
变量将由执行命令后从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.
}
在方法中返回后所做的任何操作都不会执行。i、 e.该方法以返回结束,然后将该值带回调用方法。您收到的实际错误是什么?它将它们添加到数据库中,然后给我这个错误:System.Data.dll中发生了类型为“System.StackOverflowException”的未经处理的异常。我相信它卡在了循环中。我刚刚更新以显示存储过程,并调用了刚才删除的值这一行现在得到一个构建错误:并非所有路径都返回值ExecuteNonQuery
仅返回受影响的行数。OP有一个用于exists
的输出参数,因此捕捉返回值没有帮助。@DaveZych更新了答案!我在查看存储过程后得到了它。谢谢,谢谢!非常感谢。这让我快发疯了!