OdbcConnection.GetSchema("TABLES");不工作 [英] OdbcConnection.GetSchema("TABLES"); not working
问题描述
我猜并不是所有的 SQL 都是平等创建的.我正在深入研究 C# 中的 DSN 和 ODBC 驱动程序的世界,并对其进行了一些尝试.我正在尝试获取由 DSN 定义的数据库中的所有表,我所知道的只是使用 Transoft ODBC 驱动程序.我可以连接到它并使用代码取回表:
I am guessing that not all SQL is created equally. I am diving into the world of DSNs and ODBC drivers in c# and having a bit of a go at it. I am trying to get all the tables in a database that is defined by a DSN that all I know about it is using a Transoft ODBC driver. I can connect to it and get back tables using the code:
public void ConnectToData(String dsn)
{
System.Data.Odbc.OdbcConnection conn =
new System.Data.Odbc.OdbcConnection();
//conn.ConnectionString = "FIL=MS Access;DSN=" + dsn;
conn.ConnectionString = "DSN=" + dsn; //dsn equals "Company_Shared"
try
{
conn.Open();
MessageBox.Show("Connected!");
lstBoxLogs.Items.Add("Connected");
DataTable tableschema = conn.GetSchema("TABLES");
DataSet set = tableschema.DataSet;
// first column name
for (int i = 0; i < tableschema.Columns.Count; i++)
{
lstBoxLogs.Items.Add(tableschema.Columns[i].ColumnName);
}
lstBoxLogs.Refresh();
MessageBox.Show(tableschema.Columns.Count + " tables found");
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source: " + ex.GetBaseException().Message);
}
finally
{
conn.Close();
}
}
它连接良好,并报告表但不是我知道我在数据库中查找的表返回如下:
It connects fine, and reports tables but not the tables I know I am looking for in the database what comes back is the following:
TABLE_QUALIFIER
TABLE_OWNER
TABLE_NAME
TABLE_TYPE
备注
我不确定如何从这些信息中获取实际的表名,所以我可以转储每个表中的所有数据(这就是我想要做的).这是因为我必须阅读 transoft 数据库使用什么样的 SQL 并且它是否使 conn.GetSchema("TABLES");
调用无用?
I am not sure how to get the actual table names from this information so I can just dump all the data in every table (This is all I want to do). Is this cause I have to read up on what kind of SQL a transoft database uses and does it render the conn.GetSchema("TABLES");
call useless?
推荐答案
这行得通吗?
using(DataTable tableschema = conn.GetSchema("TABLES"))
{
// first column name
foreach(DataRow row in tableschema.Rows)
{
lstBoxLogs.Items.Add(row["TABLE_NAME"].ToString());
}
}
修复了不使用数据集的代码.
Fixed the code to not use the DataSet.
编辑:为未来的读者更新了代码,以实现处理 DataTable
的最佳实践.
Updated the code for future readers to implement the best practice of disposing of the DataTable
.
这篇关于OdbcConnection.GetSchema("TABLES");不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!