gpt4 book ai didi

c# - 在 C# 中获取数据库表名

转载 作者:搜寻专家 更新时间:2023-10-30 20:56:14 29 4
gpt4 key购买 nike

好吧,我知道这个案例已经在网上讨论过了,但我还是不明白。

到目前为止,这是我的搜索结果:

"SELECT table_name AS Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = 'BASE TABLE'";

在这段 SQL 代码中,我认为 INFORMATION_SCHEMA 是一个数据库名称,当我执行代码时(当然是使用连接的数据库名称),抛出了一个异常,说数据库名称没有'存在。

更新:

当我运行 SQL 查询并执行它时:

OleDbDataReader reader = cmd.ExecuteReader();

它说:找不到:path/INFORMATION_SCHEMA.mdb;

我已经做过的并且还不错的是:

  1. 连接字符串填写数据库名称。
  2. 我可以对我的数据库执行常用的 SQL CommandText。
  3. 我正在使用 OLEDB 数据库管理系统。

已解决

在其他人的帮助下,我的最终代码是:

DataTable MySchemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[] { null, null, null, "TABLE" });

for (int i = 0; i < MySchemaTable.Rows.Count; i++) {
combobox1.Items.Add(MySchemaTable.Rows[i].ItemArray[2].ToString()); }

另一个搜索是这样的:

`DataTable T = con.GetSchema("Tables");`

当我打印出 t.ToString(); 时,它只显示表格。

还有更多搜索。

结案!

最佳答案

您不能在 OLEDB 中使用 INFORMATION_SCHEMA,它有不同的方式来提供其表信息。看看GetOleDbSchemaTable .看起来像:

MySchemaTable = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})

文档说明 here :

The .NET Framework Data Provider for OLE DB also exposes schema information by using the GetOleDbSchemaTable method of the OleDbConnection object. As arguments, GetOleDbSchemaTable takes an OleDbSchemaGuid that identifies the schema information to return, and an array of restrictions on those returned columns. GetOleDbSchemaTable returns a DataTable populated with the requested schema information.

关于c# - 在 C# 中获取数据库表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20516983/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com