gpt4 book ai didi

c# - 执行存储过程并通过输出和 sql 参数将行计数返回给代码

转载 作者:太空狗 更新时间:2023-10-29 18:25:13 25 4
gpt4 key购买 nike

我有以下代码通过 session 变量匹配用户输入。如果 session 变量中的数据与数据库中的数据匹配,则存储过程返回行计数。

除了我想返回始终是单行的行计数外,一切正常:简而言之,您访问一个表单,添加信息并点击提交。数据存储在 session 中,存储过程匹配时返回数据。

即使程序正常运行,intRecCount 变量始终为零,而不是行计数。

存储过程:

CREATE PROCEDURE [dbo].[uspConfirmation]

@RecordID CHAR(36),
@LName VARCHAR(30),
@FName VARCHAR(30),
@MInit CHAR(1),
@RecordCount INT OUTPUT
AS

SELECT * FROM Registration
WHERE RecordID = @RecordID AND
LName = @LName AND
FName = @FName AND
MInit = @MInit

SET @RecordCount = @@ROWCOUNT

RETURN

方法/代码:

public static DataSet Confirmation()
{
SqlCommand cmdSQL = new SqlCommand("uspConfirmation", Connection);
cmdSQL.CommandType = CommandType.StoredProcedure;

cmdSQL.Parameters.Add(new SqlParameter("@RecordID", SqlDbType.VarChar, 36));
cmdSQL.Parameters["@RecordID"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@RecordID"].Value = RecordIDSession;

cmdSQL.Parameters.Add(new SqlParameter("@LName", SqlDbType.VarChar, 30));
cmdSQL.Parameters["@LName"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@LName"].Value = LNameSession;

cmdSQL.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar, 30));
cmdSQL.Parameters["@FName"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@FName"].Value = FNameSession;

cmdSQL.Parameters.Add(new SqlParameter("@MInit", SqlDbType.Char, 1));
cmdSQL.Parameters["@MInit"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@MInit"].Value = MNameSession;

cmdSQL.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
cmdSQL.Parameters["@RecordCount"].Direction = ParameterDirection.Output;

...然后是一个变量,通过输出变量保存行数...

Int32 intRecCount = Convert.ToInt32(cmdSQL.Parameters["@RecordCount"].Value);

SqlDataAdapter da = new SqlDataAdapter(cmdSQL);
DataSet ds = new DataSet();
da.Fill(ds);

try {
Connection.Open();
cmdSQL.ExecuteNonQuery();
}
catch (Exception ex) {
dbMsg = ex.Message;
}
finally {
Connection.Close();
cmdSQL.Dispose();
cmdSQL.Parameters.Clear();
}
return ds;
}

最佳答案

您需要在执行查询之后而不是之前访问输出参数的值。因此,在执行查询之后和清除参数之前移动这一行,例如:

//VARIABLE TO HOLD ROW COUNT VIA OUTPUT VIARABLE 
Int32 intRecCount = Convert.ToInt32(cmdSQL.Parameters["@RecordCount"].Value);

所以你的方法代码是:

public static DataSet Confirmation()
{
SqlCommand cmdSQL = new SqlCommand("uspConfirmation", Connection);
cmdSQL.CommandType = CommandType.StoredProcedure;

cmdSQL.Parameters.Add(new SqlParameter("@RecordID", SqlDbType.VarChar, 36));
cmdSQL.Parameters["@RecordID"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@RecordID"].Value = RecordIDSession;

cmdSQL.Parameters.Add(new SqlParameter("@LName", SqlDbType.VarChar, 30));
cmdSQL.Parameters["@LName"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@LName"].Value = LNameSession;

cmdSQL.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar, 30));
cmdSQL.Parameters["@FName"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@FName"].Value = FNameSession;

cmdSQL.Parameters.Add(new SqlParameter("@MInit", SqlDbType.Char, 1));
cmdSQL.Parameters["@MInit"].Direction = ParameterDirection.Input;
cmdSQL.Parameters["@MInit"].Value = MNameSession;

cmdSQL.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
cmdSQL.Parameters["@RecordCount"].Direction = ParameterDirection.Output;



SqlDataAdapter da = new SqlDataAdapter(cmdSQL);
DataSet ds = new DataSet();
da.Fill(ds);
Int32 intRecCount = 0;
try
{
Connection.Open();
cmdSQL.ExecuteNonQuery();
//VARIABLE TO HOLD ROW COUNT VIA OUTPUT VIARABLE
intRecCount = Convert.ToInt32(cmdSQL.Parameters["@RecordCount"].Value);

}
catch (Exception ex)
{
dbMsg = ex.Message;
}
finally
{
Connection.Close();
cmdSQL.Dispose();
cmdSQL.Parameters.Clear();
}


return ds;
}

关于c# - 执行存储过程并通过输出和 sql 参数将行计数返回给代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16537390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com