从LINQ到Web Service获取0结果

时间:2013-10-30 07:50:29

标签: c# asp.net web-services linq-to-sql

我正在尝试创建一个简单的添加用户页面,该页面通过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)

1 个答案:

答案 0 :(得分:2)

documentation指定:

  

除非另有说明,否则所有系统存储过程都返回值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;
}