我的存储过程的输出参数出现间歇性空值。我想知道它是否与存储过程中的 NOLOCK 有关。它大部分时间都在工作,它会间歇性地失败。尤其是在高负载下。大多数情况下,它会返回您期望的“y”或“n”。
SqlConnection con = getCon();
SqlCommand cmd = new SqlCommand("loginRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@username", username));
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@exists", System.Data.SqlDbType.VarChar, 3, System.Data.ParameterDirection.Output, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Util.sendErrorEmail(ex.ToString());
}
finally
{
con.Close();
}
//The following line is the one that throws an "Object reference not set to an instance of an bject." exception
string userExists = cmd.Parameters["@exists"].Value.ToString();
这是存储过程:
ALTER PROCEDURE [dbo].[loginRecord]
(
@username nvarchar(100),
@exists char(1) OUTPUT
)
AS
IF EXISTS(select username from Users WITH (NOLOCK) where username = @username)
BEGIN
set @exists='y'
END
ELSE
BEGIN
set @exists='n'
--insert user account--
insert into Users (username, datejoined)
values (@username, getdate())
END
insert into Logins (username, logged)
values (@username, getdate())
GO