简体   繁体   中英

SQL level paging without total rows

Is it possible to convert the following script so that it can be called directly from ADO .NET? I don't want it to be a stored procedure, and I don't need TotRows if that makes it easier to convert.

DECLARE @startRow INT ; SET @startrow = 50
;WITH cols
AS
(
    SELECT table_name, column_name, 
        ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq, 
        ROW_NUMBER() OVER(ORDER BY table_name DESC, column_name desc) AS totrows
    FROM [INFORMATION_SCHEMA].columns
)
SELECT table_name, column_name, totrows + seq -1 as TotRows
FROM cols
WHERE seq BETWEEN @startRow AND @startRow + 49
ORDER BY seq

The only thing you'd need to do to use inline SQL from C# is to use a parameter instead of a local variable.

Like so.

using(SqlConnection cnn = GetAConnection())
{
    string sql = @"WITH cols
        AS
        (
        SELECT table_name, column_name, 
            ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq, 
            ROW_NUMBER() OVER(ORDER BY table_name DESC, column_name desc) AS totrows
        FROM [INFORMATION_SCHEMA].columns
        )
        SELECT table_name, column_name, totrows + seq -1 as TotRows
        FROM cols
        WHERE seq BETWEEN @startRow AND @startRow + 49
        ORDER BY seq";

   SqlCommand cmd = new SqlCommand(sql,cnn);
   cmd.Parameters.AddWithValue("@startRow",50);

   cnn.Open();
   using(SqlDataReader rdr = cmd.ExecuteReader())
   {

         //Do something with the reader here.
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM