Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Passing parameters to query

$
0
0

HI.
I am trying to use offset in sql query, which is to be called from a C# console application.

Query which I want to make the offset value as parameterized :  

SELECT COL_A, COL_B from tablename ORDER BY COL_A OFFSET 0 ROWS FETCH NEXT 5000 ROWS ONLY

I have tried the below code, but not sure how to pass the value as a parameter for the Offset value

 public SqlCommand CreateCommandOffSet(string qry, int offsetvalue)
        {
            qry =  qry + " ORDER BY COL_A OFFSET  XX ROWS FETCH NEXT 5000 ROWS ONLY ";
            SqlCommand cmd = new SqlCommand(qry, _conn);

                        if (_trans != null)
                cmd.Transaction = _trans;

                      cmd.CommandType = CommandType.Text;
           
            cmd.CommandText = qry;
            
            var numParam = new SqlParameter();
            numParam.ParameterName = "Offset";
            numParam.SqlDbType = System.Data.SqlDbType.Int;
            numParam.Direction = ParameterDirection.Input;
            numParam.Value = offsetvalue;

                    cmd.Parameters.Add(numParam);

            return cmd;
        }

The place where XX is put , i need the parameter. How to achieve this?

Thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>