How to set parameters in context.Query

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
wangzhi0571
Posts: 1
Joined: Thu 19 Jul 2012 02:32

How to set parameters in context.Query

Post by wangzhi0571 » Thu 19 Jul 2012 03:08

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.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: How to set parameters in context.Query

Post by MariiaI » Thu 19 Jul 2012 11:53

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

Post Reply