0

我正在尝试创建一个简单的添加用户页面,该页面通过 Web 服务、Linq 和存储过程。我已将存储过程的结果设置为布尔值。问题是它总是以虚假的形式出现。而且我看不到错误所在,因为它没有返回任何“错误”消息。

我认为我的主 .aspx 页面没有任何问题,因为它似乎将数据传递到 Web 服务。所以我只会发布其余的代码:

网络服务.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));
}

存储过程创建用户:

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。这表示成功,非零值表示失败。

如果您想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 回答