【问题标题】:How to get postgresql tablename for a given column in a joined query如何在连接查询中获取给定列的 postgresql 表名
【发布时间】:2019-08-15 21:21:10
【问题描述】:

我正在使用最新的 npgsql 来访问 postgresql V12 数据库。应用程序:用户提供任意选择,显示网格,当用户单击列标题时显示元数据(我的,不是 Postgres 的)。该元数据按表和列存储,因此我需要检索 PostgreSQL 表名和列名来检索数据。

作为参考,在 ODBC 下这是解决方案:

Debug.Print(sqlrecordset.Fields(WhichField).Properties("BASETABLENAME").Value)

我从这些结构开始:

Public M_DS As System.Data.DataSet
Dim M_Conn As Npgsql.NpgsqlConnection
Dim M_Cmd As Npgsql.NpgsqlCommand
Dim M_DA As Npgsql.NpgsqlDataAdapter

以下逻辑返回查询中列的元数据,但“basetablename”和“basecatalogname”不是 PostgreSQL 目录或基表名称。这是一个错误吗?如果没有,我正在尝试做的事情是否可能?

Dim MyDTR As System.Data.DataTableReader
MyDTR = MyDs.CreateDataReader()
Dim Splunge As DataTable = MyDTR.GetSchemaTable
For i = 0 To Splunge.Rows.Count - 1
Debug.Print("Splunge Row " & i)
For j As Integer = 0 To Splunge.Rows(i).ItemArray.Count - 1
    Try
    Debug.Print("col " & j & " -> " & Splunge.Columns(j).ColumnName & " = " & Splunge.Rows(i).Item(j).ToString)
    Catch
    End Try
Next
Next

数据库“ElDorado”中的查询是“SELECT * FROM ACTION_CODE”

输出包括:

col 14 -> BaseCatalogName = NewDataSet (this is MyDS.datasetname - should be ElDorado?)
col 15 -> BaseSchemaName = 
col 16 -> BaseTableName = Table (this is MyDs.Tables(0) name, and should be Action_Code?)
col 17 -> BaseColumnName = description

【问题讨论】:

  • 据我所知,这个以前微不足道的任务现在不可能了。

标签: vb.net postgresql metadata npgsql


【解决方案1】:

这个函数应该返回一个DataTable。将其绑定到 DataGridView 以查看作为测试返回的内容。然后您可以根据需要访问 DataTable。

参考:https://github.com/npgsql/npgsql/blob/dde72f6767e14cf1805d1394d38bd9aeca96bd64/src/Npgsql/NpgsqlConnection.cs#L1296

https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_GetSchema_System_String_

Private Function GetTables() As DataTable
    Dim dt As DataTable
    Using M_Conn As New Npgsql.NpgsqlConnection("Your connection string")
        dt = M_Conn.GetSchema("Tables")
    End Using
    Return dt
End Function

【讨论】:

  • 试过这个,然后遍历 dt 中的行和列。这些显示了系统中所有表的列表,但似乎与当前查询无关。Dim Dt As DataTable Dt = MyConn.GetSchema("Tables") For i = 0 To Dt.Columns.Count - 1 Print("Column " & i & " " & Dt.Columns(i).ColumnName) Next Print("Done with that.") For i = 0 To Dt.Rows.Count - 1 Dim j As Integer For j = 0 To Dt.Columns.Count - 1 Try Print("Row " & i & " col " & j & " is type " & TypeName(Dt.Rows(i).Item(j))) Print("Row " & i & " col " & j & " = " & Dt.Rows(i).Item(j)) End Try Next Next
  • 抱歉,我无法访问 Postgres,因此无法进行测试。在这里可能会有所帮助。 dba.stackexchange.com/questions/22362/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 2022-01-24
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
相关资源
最近更新 更多