Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何捕获由.asmx文件中的RAISEERROR()触发的SQL存储过程错误?_.net_Sql_Stored Procedures_Asmx_Raiseerror - Fatal编程技术网

.net 如何捕获由.asmx文件中的RAISEERROR()触发的SQL存储过程错误?

.net 如何捕获由.asmx文件中的RAISEERROR()触发的SQL存储过程错误?,.net,sql,stored-procedures,asmx,raiseerror,.net,Sql,Stored Procedures,Asmx,Raiseerror,我对.NET非常陌生。到目前为止,我所做的只是通过复制和粘贴现有代码(按照上级的指示),将一些SQL存储过程调用实现为Web服务 我正在通过数据库.ExecuteNonQuery()调用一些命令。在SQL存储过程中,我根据传入的LoginId执行一些安全检查(用户只能编辑其拥有的项)。如果安全检查失败,我将调用RAISEERROR()来触发一个错误,但我的.asmx文件没有将此视为异常,而是直接退出存储过程 我的asmx文件: [WebMethod] public XmlDocument Upd

我对.NET非常陌生。到目前为止,我所做的只是通过复制和粘贴现有代码(按照上级的指示),将一些SQL存储过程调用实现为Web服务

我正在通过
数据库.ExecuteNonQuery()
调用一些命令。在SQL存储过程中,我根据传入的LoginId执行一些安全检查(用户只能编辑其拥有的项)。如果安全检查失败,我将调用RAISEERROR()来触发一个错误,但我的.asmx文件没有将此视为异常,而是直接退出存储过程

我的asmx文件:

[WebMethod]
public XmlDocument UpdateSomeItem(Int32 ItemIdNmb, String Foo)
{
    try
    {

        // Create the Database object, using the default database service. The
        // default database service is determined through configuration.
        Database db = DatabaseFactory.CreateDatabase();
        DbParameter param;

        string sqlCommand = "updateSomeItem";
        DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);

        db.AddInParameter(dbCommand, "ItemIdNmb", DbType.Int32, ItemIdNmb);
        db.AddInParameter(dbCommand, "Foo", DbType.String, Foo);
        db.AddInParameter(dbCommand, "UpdateLoginIdNmb", DbType.Int32, SessionLoginIdNmb);

        #region Return Parameter
        param = dbCommand.CreateParameter();
        param.ParameterName = "Return";
        param.Direction = ParameterDirection.ReturnValue;
        param.DbType = DbType.Int32;

        dbCommand.Parameters.Add(param);
        #endregion

        // Execute the Command
        db.ExecuteNonQuery(dbCommand);

        myDataSet = new DataSet();

        myDataSet.DataSetName = "DataSet";
        myDataSet.Tables.Add(errorDataTable);

        statusDataRow["Status"] = "Success";
        statusDataTable.Rows.Add(statusDataRow);
        myDataSet.Tables.Add(statusDataTable);

        returncodeDataRow["ReturnCode"] = dbCommand.Parameters["Return"].Value;
        returncodeDataTable.Rows.Add(returncodeDataRow);
        myDataSet.Tables.Add(returncodeDataTable);

        xmlReturnDoc.LoadXml(myDataSet.GetXml());

        return xmlReturnDoc;
    }
    // THIS IS WHERE I WANT MY RAISE ERROR TO GO
    catch (Exception e) 
    {
        return ReturnClientError(e, "someerror");
    }
}
我的SQL存储过程:

CREATE PROCEDURE updateSomeItem
(
    @ItemIdNmb             INT,
    @Foo                   VARCHAR(100),
    @UpdateLoginIdNmb      INT
)

AS

SET NOCOUNT ON

/* If the user performing the action did not create the Item in question, 
    raise an error. */
IF NOT EXISTS
    (
    SELECT 1
    FROM Items
    WHERE IdNmb = @ItemIdNmb
        AND LoginIdNmb = @UpdateLoginIdNmb
    )
    RAISERROR (
        'User action not allowed - User is not the owner of the specified Item', 
        10, 
        1)

UPDATE Items
SET Foo = @Foo
WHERE IdNmb = @ItemIdNmb

RETURN 0

SET NOCOUNT OFF
GO

问题是错误严重性太低。严重程度1-10被视为信息消息,不会中断C#app的流程


可以在from中找到更全面的解释。

问题在于错误严重性太低。严重程度1-10被视为信息消息,不会中断C#app的流程


更详细的解释可以在from中找到。

我用以下内容更新了我的存储过程:

/* If the user performing the action did not create the Item in question, 
    return a code other than 0. */
IF NOT EXISTS
    (
    SELECT 1
    FROM Items
    WHERE IdNmb = @ItemIdNmb
        AND LoginIdNmb = @UpdateLoginIdNmb
    )
    RETURN 1
我在.asmx文件中的处理方式如下:

int returnValue = (int)dbCommand.Parameters["Return"].Value;

if (returnValue == 0)
    statusDataRow["Status"] = "Success";
/* Stored Procedure will return a value of "1" if the user is 
    attempting to update an Item that they do not own. */
else
    statusDataRow["Status"] = "Error";

statusDataTable.Rows.Add(statusDataRow);
myDataSet.Tables.Add(statusDataTable);

returncodeDataRow["ReturnCode"] = dbCommand.Parameters["Return"].Value;
returncodeDataTable.Rows.Add(returncodeDataRow);
myDataSet.Tables.Add(returncodeDataTable);

我用以下内容更新了存储过程:

/* If the user performing the action did not create the Item in question, 
    return a code other than 0. */
IF NOT EXISTS
    (
    SELECT 1
    FROM Items
    WHERE IdNmb = @ItemIdNmb
        AND LoginIdNmb = @UpdateLoginIdNmb
    )
    RETURN 1
我在.asmx文件中的处理方式如下:

int returnValue = (int)dbCommand.Parameters["Return"].Value;

if (returnValue == 0)
    statusDataRow["Status"] = "Success";
/* Stored Procedure will return a value of "1" if the user is 
    attempting to update an Item that they do not own. */
else
    statusDataRow["Status"] = "Error";

statusDataTable.Rows.Add(statusDataRow);
myDataSet.Tables.Add(statusDataTable);

returncodeDataRow["ReturnCode"] = dbCommand.Parameters["Return"].Value;
returncodeDataTable.Rows.Add(returncodeDataRow);
myDataSet.Tables.Add(returncodeDataTable);

谢谢@ahsteele。这是有道理的。我改变了逻辑,在我描述的条件下返回不同的代码,并在我的C#(见下文)中处理返回代码。谢谢@ahsteele。这是有道理的。我改变了逻辑,在我描述的条件下返回不同的代码,并在我的C#中处理返回代码(见下文)。