从过程中获取resultSet和ReturnedValue

时间:2013-12-05 09:10:18

标签: c# sql sql-server oledb

我需要从过程中获取resultSet和ReturnedValue。

我的问题是,据我所知获得返回值,我需要执行“ExecuteNonQuery()”,对于读者我需要执行“ExecuteReader()”。 但这似乎是正确的,我称之为执行2次。

这样做的当前方式是什么

using (OleDbConnection connection = new OleDbConnection(getConnetionString()))
{
    using (OleDbCommand command = new OleDbCommand())
    {
        command.CommandText = "GamesApp.Images_Get";
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;

        OleDbParameter retParameter = DBUtils.createReturnOleParam(OleDbType.Integer);
        command.Parameters.Add(retParameter);
        tmpParameter = DBUtils.createDBParam("@ImageID", OleDbType.Integer, ParameterDirection.Input, id);
        command.Parameters.Add(tmpParameter);
        command.Connection.Open();
        command.ExecuteNonQuery();

        int retValue = (int) retParameter.Value;

        if (retValue == 0)
            using (OleDbDataReader reader = command.ExecuteReader())
            {
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        Debug.WriteLine(reader.GetString(1));
                    }
                    reader.Close();
                }
            }

        connection.Close();
    }
}    

GamesApp.Images_Get“:

ALTER PROCEDURE [GamesApp].[Images_Get]
@ImageID            SMALLINT
AS
BEGIN

SET NOCOUNT ON;

DECLARE @ErrorProc          VARCHAR(500), @ErrorNumber INT, @ErrorSeverity INT, @ErrorState INT, @ErrorLine INT, @ErrorMessage VARCHAR(512)
DECLARE @ProcParams         NVARCHAR(4000);


BEGIN TRY

    --Check if @ImageID exists.if not error code is 1
    IF NOT EXISTS (SELECT * FROM GamesApp.Images WHERE ImageID = @ImageID)
        RETURN 1

    SELECT ImageName, BG_Color, DisplayText, Text_Color, Text_Align, Text_Size
            , ImageFormat, HtmlImageType, TheImage
    FROM GamesApp.Images
    WHERE ImageID = @ImageID;


    RETURN 0;
END TRY
BEGIN CATCH


    SELECT
       @ErrorNumber = ERROR_NUMBER(),
       @ErrorSeverity = ERROR_SEVERITY(),
       @ErrorState = ERROR_STATE(),
       @ErrorLine = ERROR_LINE(),
       @ErrorMessage = ERROR_MESSAGE(),
       @ErrorProc = 'GamesApp.Images_Get',
       @ProcParams = '@ImageID' + @ImageID 
    EXEC dbo.Log_Exceptions_Insert @ErrorProc, @ProcParams, @ErrorLine, @ErrorNumber, @ErrorSeverity, @ErrorState,  @ErrorMessage;

    THROW;

END CATCH

END

1 个答案:

答案 0 :(得分:0)

我认为无需致电ExecuteNonQuery()

如@Damith所述,您可以使用OleDbDataReader.HasRows

要进行验证,您可以查看herehere