OdbcConnection.GetSchema(“TABLES”); not working

元气小坏坏 提交于 2019-12-11 08:18:52

问题


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 REMARKS

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?


回答1:


Would this work?

using(DataTable tableschema = conn.GetSchema("TABLES"))
{
    // first column name
    foreach(DataRow row in tableschema.Rows)
    {
        lstBoxLogs.Items.Add(row["TABLE_NAME"].ToString());
    }
}

EDIT: Fixed the code to not use the DataSet.

EDIT: Updated the code for future readers to implement the best practice of disposing of the DataTable.



来源:https://stackoverflow.com/questions/8695349/odbcconnection-getschematables-not-working

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