jbrabant
jbrabant

Reputation: 345

ServiceStack OrmLite SqlList<object>

I have a request that takes a stored procedure name with a list of parameters. It could be any SP so the result could be a list of anything and that is why I use SqlList<object>.

When I use

return db.SqlList<object>(spName, cmd =>
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        if (parameters != null)
                        {
                            foreach (var p in parameters)
                            {
                                cmd.AddParam(p.Key, p.Value, ParameterDirection.Input);
                            }
                        }
                        cmd.CommandTimeout = 90;
                    });

I get a system.dynamic.expendo object as: {[result, 1.7783336]} On the client, I want to get the decimal value but I struggle... I created a class on the client that has a decimal property "result" and tried to convert to it but it doesn't work. I tried to take that string and convert it using FromJson and it doesn't work either...

Upvotes: 1

Views: 566

Answers (1)

mythz
mythz

Reputation: 143399

See the docs for the recommended APIs for calling stored procedures with OrmLite.

The return type needs to match what the Stored Procedure returns, if the SP just returns a decimal you should be able to access it with:

var result = db.SqlScalar<decimal>("EXEC MyProc @p1 @p2", new { p1, p2 });

Or if you don't know what Type it will return as you can use object, e.g:

var result = db.SqlScalar<object>("EXEC MyProc @p1 @p2", new { p1, p2 });

If it doesn't return a naked scalar result you'll need to match the shape of the return type, e.g. if it's returning a single row you can access the row values in a List<object> with:

var result = db.Single<List<object>>("EXEC MyProc @p1 @p2", new { p1, p2 });

Upvotes: 1

Related Questions