Я получаю прерывистое значение NULL для выходного параметра хранимой процедуры, которую я имею.Мне интересно, связано ли это с 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