0

Web サービス、Linq、およびストアド プロシージャを通過する単純な [ユーザーの追加] ページを作成しようとしています。ストアド プロシージャの結果をブール値として返すように設定しました。問題は、常に偽として戻ってくることです。また、「エラー」メッセージが返されないため、エラーがどこにあるのかわかりません。

メインの .aspx ページはデータを通過して Web サービスに到達しているように見えるので、問題はないと思います。したがって、残りのコードのみを投稿します。

WebService.cs:

   public bool AddUser(string Name, int Contact, string Email, string Password, bool Admin, string ImageURL)
{
    bool result = false;

        using (DataClassesDataContext dbcontext = new DataClassesDataContext())
        {
            var query = dbcontext.CreateUser(Name, Contact, Email, Password, Admin, ImageURL); //as far as I can see, the values get passed through these parameters. But the query returns false.
            result = Convert.ToBoolean(query);
        }

    return result;
}

DataContext.designer.cs:

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.CreateUser")]
public int CreateUser([global::System.Data.Linq.Mapping.ParameterAttribute(Name="Name", DbType="VarChar(50)")] string name, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="ContactNo", DbType="Int")] System.Nullable<int> contactNo, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Email", DbType="VarChar(50)")] string email, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Password", DbType="VarChar(50)")] string password, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Admin", DbType="Bit")] System.Nullable<bool> admin, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="ImageURL", DbType="NVarChar(200)")] string imageURL)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), name, contactNo, email, password, admin, imageURL);
    return ((int)(result.ReturnValue));
}

ストアド プロシージャ CreateUser:

ALTER PROCEDURE dbo.CreateUser
@Name varchar(50),
@ContactNo int,
@Email varchar(50),
@Password varchar(50),
@Admin bit,
@ImageURL nvarchar(200)
AS
INSERT INTO Users
([Name], ContactNo, Email, Password, Admin, ImageURL)
VALUES
(@Name, @ContactNo, @Email, @Password, @Admin, @ImageURL)
4

1 に答える 1

2

ドキュメントでは次のように指定されています。

特に記載がない限り、すべてのシステム ストアド プロシージャは値 0 を返します。これは成功を示し、0 以外の値は失敗を示します。

trueストアド プロシージャから明示的に戻りたい場合は、このコードを使用します

ALTER PROCEDURE dbo.CreateUser @Name      VARCHAR(50),
                               @ContactNo INT,
                               @Email     VARCHAR(50),
                               @Password  VARCHAR(50),
                               @Admin     BIT,
                               @ImageURL  NVARCHAR(200)
AS
    INSERT INTO Users
                ([Name],
                 ContactNo,
                 Email,
                 Password,
                 Admin,
                 ImageURL)
    VALUES      (@Name,
                 @ContactNo,
                 @Email,
                 @Password,
                 @Admin,
                 @ImageURL)

    RETURN 1 

アップデート

上記のアプローチは絶対確実ではありません。挿入が失敗した場合、プロシージャはエラー ステータス コードを返しませんが、メソッドが成功インジケータ ( false) を返さないようにする例外をスローします。はるかに優れたアプローチは、ストアド プロシージャからステータス コードを返さず、最終的な例外をコードで処理することです。

public bool AddUser(string Name, int Contact, string Email, string Password, bool Admin, string ImageURL)
{
    using (DataClassesDataContext dbcontext = new DataClassesDataContext())
    {
        try
        {
            var query = dbcontext.CreateUser(Name, Contact, Email, Password, Admin, ImageURL); 
            // If the execution got here then the insert succeeded
            // and you can return 'success' to caller
            return true;
        }
        catch(Exception ex)
        {
            // Log the exception
            // If the execution got here it means that the insert failed and 
            // an exception was thrown (in other words the execution didn't get to 
            // the <return true;> instruction.
            // In this case return 'fail' to caller.
            return false;
        }

    }

    return result;
}
于 2013-10-30T08:27:12.987 に答える