in

System.Data.SQLite

An open-source, enhanced version of the SQLite database engine for Windows

How to retrieve the column name?

Last post 07-12-2006 9:16 AM by Robert Simpson. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 07-11-2006 1:04 PM

    How to retrieve the column name?

    Can anyone tell me how i can get the column names of a specific table?
  • 07-11-2006 1:19 PM In reply to

    Re: How to retrieve the column name?

    using (DbConnection connection = new SQLiteConnection("Data Source=mydb.db3"))
    {
      connection.Open();
      DataTable tableInfo = connection.GetSchema("Columns", new string[] { null, null, "MyTable" });

      // Examine tableInfo to get all the schema information on the table
    }

     

  • 07-12-2006 6:44 AM In reply to

    Re: How to retrieve the column name?

    DataTable tableInfo = this.sqlite_conn.GetSchema("Columns", new string[] {null, null, "aodb"});
    DataColumnCollection columns = tableInfo.Columns;
    foreach(DataColumn test in columns) {
    column = test.ColumnName;
    _MainForm.WriteToInfoWindow(column + "\n");
    }

    What is wrong with this code? I am getting only table_catalog, table_schema, table_name, column_name, column_guid as output and not the column names.

    Btw. I am new to C# so pls be patient with me :)
  • 07-12-2006 7:47 AM In reply to

    Re: How to retrieve the column name?

    Nothing's wrong with the code, but you're outputting the wrong information.

    Try this instead:

    DataTable tableInfo = this.sqlite_conn.GetSchema("Columns", new string[] {null, null, "aodb"});
    DataColumnCollection columns = tableInfo.Columns;
    foreach(DataRow test in columns.Rows) {
    _MainForm.WriteToInfoWindow((string)test["column_name"] + "\n");
    }

  • 07-12-2006 8:04 AM In reply to

    Re: How to retrieve the column name?

    Getting the error message while trying to compile your code:
    "System.Data.DataColumnCollection" includes no Definition for "Rows".
  • 07-12-2006 8:49 AM In reply to

    Re: How to retrieve the column name?

    why complicate when you can do it easy:
    sqlite_datareader.GetName(i).ToString()
    (i is the id of the column)

    anyway thx for the help :)
  • 07-12-2006 9:16 AM In reply to

    Re: How to retrieve the column name?

    Sorry, should've been:

    DataTable tableInfo = this.sqlite_conn.GetSchema("Columns", new string[] {null, null, "aodb"});
    foreach(DataRow test in tableInfo.Rows) {
    _MainForm.WriteToInfoWindow((string)test["column_name"] + "\n");
    }

    You asked for a method of getting the column names from a table, not a method of how to get the column names for a query.   If you want to get the names of columns in a table without executing a datareader and making the database setup to fetch data rows, then the above schema method is the best route.  If on the other hand, you're running a query already and just need column names, then the datareader (as you posted) method is the better route.

    Robert

     

Page 1 of 1 (7 items)
Powered by Community Server (Commercial Edition), by Telligent Systems