我有以下代码通过会话变量匹配用户输入。如果会话变量中的数据与数据库中的数据匹配,存储过程将返回行计数。
一切正常,除了我想返回始终为单行的行数:简而言之,您访问一个表单,添加信息并点击提交。数据存储在会话中,存储过程在匹配时返回数据。
即使程序正常工作,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;
}