2

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

2 に答える 2

1

私の推測では、@exists に値が割り当てられる前に例外が発生しています。キャッチを次のように変更します。

Catch(Exception ex) { Util.sendErrorEmail(ex.ToString()); return; }

于 2011-08-12T16:03:15.307 に答える
0

ストアドプロシージャの実装とは関係ないと思います。ストアド プロシージャから意図的に @exists 変数に null を返す場合、"cmd.Parameters["@exists"].Value" は C# で null にはなりません。代わりに、有効なオブジェクトである「System.DBNull」になり、メソッドを呼び出すことができます。

これは質問に対する直接的な回答ではありませんが、質問を絞り込むのに役立ちます。

于 2011-08-13T03:04:52.123 に答える