简体   繁体   中英

Does anyone know of a way to paginate a call to GetSchema from C#?

I'm using the ADO.NET provider function "GetSchema" to fetch meta data out of a Sql Server database (and an Informix system as well) and want to know if there is anyway to paginate the results. I ask because one of the systems has over 3,000 tables (yes, three thousand) and twice that many views and let's not even talk about the stored procedures. Needless to say, trying to bring down that list in one shot is too much for the VM I have running (a mere 4GB of memory). I'm already aware of the restrictions that can be applied, these are all tables in the "dbo" schema so there isn't much else that I'm aware of for limiting the result set before it gets to my client.

Instead of using GetSchema I suggest to use the more flexible INFORMATION_SCHEMA system views. These views already divide the information about the Tables, StoredProcedures and Views and you can write a specific query to retrieve your data in a paginated way.

For example to retrieve the first 100 rows of table names you could write a query like this

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY TABLE_NAME) AS RowNum, *
          FROM      INFORMATION_SCHEMA.TABLES
        ) AS TableWithRowNum
WHERE   RowNum >= 0
    AND RowNum < 100
ORDER BY RowNum

Following queries could be easily prepared changing the min and max values used by the query.

The same code could be applied for StoredProcedures (using INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' ) or the views using INFORMATION_SCHEMA.VIEWS

Note, if you are using Sql Server 2012 and later the first query could be rewritten to use this syntax

SELECT * 
FROM INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_NAME 
OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY

And the C# code could also use parameters for the FIRST (0) and COUNT(100) values

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