ストアド プロシージャの出力パラメータで断続的に 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