how to let users to select columns in a database table

不想你离开。 提交于 2019-12-13 01:14:45

问题


We are trying to build an web application that allows users to select appropriate columns for given database table.

I wonder if there is any API for these - I've googled it a while in vain. Otherwise, if you can give some clues (patterns or sample codes) how to build such a component, that will be great and appreciated.


回答1:


You could base your application on INFORMATION_SCHEMA views/table. This is documentation for SQL Server, but you can easily find it for other databases too:

http://msdn.microsoft.com/en-us/library/ms186778.aspx

Sample SQLs:

select * from INFORMATION_SCHEMA.TABLES

select * from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'users'

if you want to use this solution with many databases, to separate your application from db engine, you can create about defining IMetadataProvider interface and create implementations for different databases:

interface IMetadataProvider {
    ...GetTables();
    ...GetTableColumns();
    ...GetTableRelations();
    //Other functions required by your project
}

You can also create your own query builder interface:

interface IQueryBuilder {
    ...From(string tableName);
    ...Top(int numberOfRows); //TOP for SQL SERVER, LIMIT for MySQL
} 


来源:https://stackoverflow.com/questions/7123326/how-to-let-users-to-select-columns-in-a-database-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!