如何让用户选择数据库表中的列

发布于 2024-11-30 11:15:30 字数 138 浏览 3 评论 0原文

我们正在尝试构建一个 Web 应用程序,允许用户为给定的数据库表选择适当的列。

我想知道是否有针对这些的 API - 我在谷歌上搜索了一段时间,但没有成功。否则,如果您可以提供一些如何构建此类组件的线索(模式或示例代码),那就太好了并且值得赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-12-07 11:15:30

您可以将您的应用程序基于 INFORMATION_SCHEMA 视图/表。这是 SQL Server 的文档,但您也可以轻松找到其他数据库的文档:

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

示例 SQL:

select * from INFORMATION_SCHEMA.TABLES

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

如果您想将此解决方案与多个数据库一起使用,要将应用程序与数据库引擎分开,您可以创建有关定义IMetadataProvider 接口并为不同的数据库创建实现:

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

您还可以创建自己的查询生成器接口:

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

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
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文