ExecuteSqlCommand with output parameter

前端 未结 6 1010
名媛妹妹
名媛妹妹 2020-12-06 04:43

I\'m using Entity Framework in an ASP.NET MVC3 application and I\'m trying to use the following code:

var token = \"\";
this.Database.ExecuteSqlCommand(\"exe         


        
相关标签:
6条回答
  • 2020-12-06 05:00
    var outParam = new SqlParameter();
    outParam.ParameterName = "OutPutParametname";
    outParam.SqlDbType = SqlDbType.Bit;//DataType Of OutPut Parameter
    outParam.Direction = ParameterDirection.Output;
    db.Database.ExecuteSqlCommand("EXEC ProcedureName @Param1,@Param2 OUTPUT", new SqlParameter("Param1", value), outParam);
    object outParamValue = Convert.ToBoolean(outParam.Value);
    
    0 讨论(0)
  • 2020-12-06 05:03

    I ended up using this to get it working, but I'm sure there's a more optimal way:

    var p = new SqlParameter
    {
        ParameterName = "token",
        DbType = System.Data.DbType.String,
        Size = 100,
        Direction = System.Data.ParameterDirection.Output
    };
    var resp = this.Database.SqlQuery<String>("exec dbo.usp_GetRequestToken @token", p);
    
    return resp.First();
    
    0 讨论(0)
  • 2020-12-06 05:08

    Below is what I do for Oracle using the DevArt driver. I have a package.proc called P_SID.SID_PGet that returns a single string value. The proc is:

    PROCEDURE SID_PGet(io_SID OUT varchar2) is
    Begin
       io_SID:=GetSID; -- GetSID just goes off and gets the actual value
    End;
    

    Below is how I call it and retrieve the SID value (I'm using this with EF 4.1 code first and this method is in the DbContext):

    /// <summary>
    /// Get the next SID value from the database
    /// </summary>
    /// <returns>String in X12345 format</returns>
    public string GetNextSId()
    {
        var parameter = new Devart.Data.Oracle.OracleParameter("io_SID", Devart.Data.Oracle.OracleDbType.VarChar, ParameterDirection.Output);
        this.Database.ExecuteSqlCommand("BEGIN P_SID.SID_PGet(:io_SID); END;", parameter);
        var sid = parameter.Value as string;
    
        return sid;
    }
    
    0 讨论(0)
  • 2020-12-06 05:11

    I solved this issue with following SQL and Entity Framework code

    SP :

        ALTER PROCEDURE [dbo].[SaveSingleColumnValueFromGrid]
    (
        @TableName VARCHAR(200),
        @ColumnName VARCHAR (200),
        @CompareField VARCHAR(200),
        @CompareValue VARCHAR(200),
        @NewValue VARCHAR(200),
        @Result INT OUTPUT
    )
    AS
    BEGIN
        DECLARE @SqlString NVARCHAR(2000),
                @id INTEGER = 0;
    
    
        IF @CompareValue = ''
            BEGIN
    
                SET @SqlString = 'INSERT INTO ' + @TableName + ' ( ' + @ColumnName +  ' )  VALUES ( ''' + @NewValue + ''' ) ; SELECT @id = SCOPE_IDENTITY()';
                EXECUTE sp_executesql @SqlString, N'@id INTEGER OUTPUT',  @id OUTPUT
            END
        ELSE
            BEGIN
                SET @SqlString = 'UPDATE ' + @TableName + ' SET ' + @ColumnName +  ' = ''' + @NewValue + '''  WHERE ' + @CompareField +  ' = ''' + @CompareValue + '''';
                EXECUTE sp_executesql @SqlString
                set @id = @@ROWCOUNT
            END
    
    
        SELECT @Result = @id
    END
    

    Entity Framework Code :

    public FieldUpdateResult SaveSingleColumnValueFromGrid(string tableName, string tableSetFieldName, string updatedValue, string tableCompareFieldName, string uniqueFieldValue)
            {
    
                var fieldUpdateResult = new FieldUpdateResult() ;
                var isNewRecord = false;
    
                if (string.IsNullOrWhiteSpace(uniqueFieldValue))
                {
                    uniqueFieldValue = string.Empty;
                    isNewRecord = true;
                }
                using (var dbContext = new DBEntities())
                {
                    var resultParameter = new SqlParameter("@Result", SqlDbType.Int)
                    {
                        Direction = ParameterDirection.Output
                    };
    
                    var recordsAffected = dbContext.Database.ExecuteSqlCommand("SaveSingleColumnValueFromGrid @TableName,@ColumnName,@CompareField,@CompareValue,@NewValue,@Result out",
                        new SqlParameter("@TableName", tableName),
                        new SqlParameter("@ColumnName", tableSetFieldName),
                        new SqlParameter("@CompareField", tableCompareFieldName),
                        new SqlParameter("@CompareValue", uniqueFieldValue),
                        new SqlParameter("@NewValue", updatedValue),
                        resultParameter);
    
                    fieldUpdateResult.Success = recordsAffected > 0;
                    if (isNewRecord)
                    {
                        fieldUpdateResult.NewId = (int)resultParameter.Value;
                    }
                    else
                    {
                        fieldUpdateResult.AffectedRows = (int)resultParameter.Value;
                    }
                }
    
                return fieldUpdateResult;
            }
    
    0 讨论(0)
  • 2020-12-06 05:13
    var db = new DBContext();
    var outParam = new SqlParameter
    {
        ParameterName = "@Param",
        DbType = System.Data.DbType.String,
        Size = 20,
        Direction = System.Data.ParameterDirection.Output
    };
    var r = db.Database.ExecuteSqlCommand("EXEC MyStoredProd @Param OUT",outParam );
    Console.WriteLine(outParam.Value);
    

    The main part i see everyone is missing, is the OUT keyword needed after @Param.

    0 讨论(0)
  • 2020-12-06 05:15

    You need to indicate the direction in the parameter. For example, try something like this:

    var p = new SqlParameter("token", token);
    p.Direction = ParameterDirection.InputOutput;
    this.Database.ExecuteSqlCommand("exec dbo.MyUsp", p);
    
    0 讨论(0)
提交回复
热议问题