Page 1 of 1

How to set parameters in context.Query

Posted: Thu 19 Jul 2012 03:08
by wangzhi0571
This is the example:

Code: Select all

string sql = SELECT * FROM POSITION where IDRUN=:PARAMETER_IDRUN ;
IQueryable<T> results = context.Query<T>(sql);
I'd like to set parameters (:PARAMETER_IDRUN) in context.Query<T>, but how can I do this?

I tried

Code: Select all

string command = string.Format("declare "
                 + "PARAMETER_IDRUN number; "
                 + "begin "
                 + "PARAMETER_IDRUN := -172; "
                 + " End;");
context.ExecuteCommand(command); 
then run

Code: Select all

IQueryable<T> results = context.Query<T>(sql);
but failed, it shows ORA-01008: not all variables bound.

PS: I know there is another way to set Parameters like:

Code: Select all

            OracleCommand ocmd = oc.CreateCommand();
            ocmd.ParameterCheck = true;
            ocmd.CommandText = "select :p1,:p2 from dual";
            ocmd.Prepare();
            ocmd.Parameters["p1"].Value = "00000";
            ocmd.Parameters["p2"].Value = "00001";
            OracleDataReader or = ocmd.ExecuteReader(); 
but it looks like not suitable for me because I need to return IQuerable<T> and use deferred execution.

Re: How to set parameters in context.Query

Posted: Thu 19 Jul 2012 11:53
by MariiaI
The DataContext.Query<T>() method has overloads that take both query text and parameter values.
Try rewriting your code as follows:

Code: Select all

string sql = @"SELECT * FROM ""POSITION"" where ""IDRUN""={0}";
int PARAMETER_IDRUN = -172;
IQueryable<T> results = context.Query<T>(sql,PARAMETER_IDRUN);
For more information about the Query method please refer to
http://www.devart.com/linqconnect/docs/ ... Query.html