0

I don't know how to store the column names from a SQLite table into a list of strings. The following code fills a dataGridView with the column names (amongst other things):

string sDatabasePath = DBPath();
SQLiteConnectionStringBuilder datasource = new SQLiteConnectionStringBuilder();
datasource.Add("Data Source", sDatabasePath);
datasource.Add("Version", "3");
datasource.Add("New", "False");
datasource.Add("Compress", "True");             
using (SQLiteConnection connection = new SQLiteConnection(datasource.ConnectionString))
{
    connection.Open(); //opens connection
    SQLiteCommand getColumnNames = new SQLiteCommand("PRAGMA table_info('myTable');", connection);
    SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(getColumnNames);
    DataSet myDataSet = new DataSet();
    //myAdapter.Fill(myDataSet, "name");
    this.dataGridView1.DataSource = myDataSet;
    this.dataGridView1.DataMember = "name";
    connection.Close();
}
Nick_F
  • 1,103
  • 2
  • 13
  • 30
  • The tables name add normally in datagrid? – Dietrich Prg Jan 20 '14 at 11:07
  • In the datagrid I get the column names correctly (first column in the datagrid is called name and on each row is the name of a column in my table. Second column in datagrid is called cid, third type, fourth not_null, etc). – Nick_F Jan 20 '14 at 11:11

2 Answers2

1

If you are looking to bind your query to a list and not a DataGridView, then you should use a data reader and not a data set e.g.

using (SQLiteConnection connection = new SQLiteConnection(datasource.ConnectionString))
using (SQLiteCommand cmd = new SQLiteCommand("PRAGMA table_info('myTable');"))
{
    connection.Open(); //opens connection
    var tableNames = new List<string>();
    using (SQLiteDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            tableNames.Add(reader.GetString(0)); // read 'name' column
        }
    }
    return tableNames;
}
James
  • 80,725
  • 18
  • 167
  • 237
  • Thank you James. You are right about using the data reader. I tried using it, but I didn't know the syntax. I just started using C#. There is a minor change I had to do - tableNames.Add(Convert.ToString(reader["name"])) – Nick_F Jan 20 '14 at 11:22
  • @Nick_F ah yeah forgot it returns `object` and not string, alternatively you can call `reader.GetString(0)` where `0` is the column number or `reader["name"].ToString()` however the latter would mean it would fail if the `name` was `null`. – James Jan 20 '14 at 11:44
1
            DataTable dtb = new DataTable();
            myAdapter.Fill(dtb);

            string[] names = new string[dtb.Rows.Count];
            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                DataRow row = dtb.Rows[i];
                names[i] = row[0].ToString();
            }
  • Thank you Dietrich for help. I went with the solution proposed by James, since he posted it first and it worked for me. – Nick_F Jan 20 '14 at 11:30
  • No problem ^-^ we are here to try help. – Dietrich Prg Jan 20 '14 at 11:36
  • @DietrichPrg you could do this in less code, `var names = dtb.Rows.Select(x => x.Item["name"]);`. I went for the `SQLiteDataReader` because I was under the assumption the OP didn't need a `DataSet`. However, if the OP is already using a `DataSet` this would be the more efficient option. – James Jan 20 '14 at 11:47
  • @James i know this, but I had some experiences with DataReader, and for my opinion, this componet have a problem, need keep the connection with the server, to me, its a problem, because with you work with server far from you and you dont have a nice internet link, what its hard to have in Brasil, the component spent more time than bring everything to memory in DataTable and work with it. – Dietrich Prg Jan 20 '14 at 11:56
  • @DietrichPrg it's not so much a problem but an implementation detail. Both serve a purpose and you shouldn't blindly say one is better than the other, it really depends on the situation. There is a nice analogy of the difference between the 2 in [this answer](http://stackoverflow.com/questions/1083193/whats-better-dataset-or-datareader#1083223). – James Jan 20 '14 at 12:13
  • @James "and for my opinion, this componet have a problem, need keep the connection with the server, to me, its a problem", i never told one is better than other, here have limitations in my country, so i need work through it, so, how i dont know the internet link of 'Nick_F' i proposed a code that works well to me. Both code works and do the same thing using differents ways with positive and negative points. – Dietrich Prg Jan 20 '14 at 12:22