0

i can write and execute something like this with sqlite3 command (in command prompt):

sqlite3 -header sms.db "select * from message"

or i found changing default setting of sqlite in Change SQLite default settings.

but i'm using c# and System.Data.SQLite.dll and then i haven't any of these solution... how can i set headers on ?

my c# code is like this:

SQLiteConnection connection = new SQLiteConnection("Data Source=" + path);

connection.Open();

SQLiteDataReader reader = new SQLiteCommand(connection) { CommandText = "select * from message" }.ExecuteReader();

while (reader.Read())
{
    //...
}

reader.Close();
connection.Close();
Community
  • 1
  • 1
Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42

1 Answers1

1

You "can't". The headers cannot be turned on through any option in SQLite.

You can however retrieve the column names from the reader and display them yourself.

SQLiteConnection connection = new SQLiteConnection("Data Source=" + path);

connection.Open();

SQLiteDataReader reader = new SQLiteCommand(connection) { CommandText = "select * from message" }.ExecuteReader();

bool needHeader = true;
while (reader.Read())
{
    // show header if this is the first row
    if(needHeader){
        ShowHeader(reader);
        needHeader = false;
    }
    //...
}

reader.Close();
connection.Close();

void ShowHeader(SQLiteDataReader reader){
    for (int i=0;i < reader.FieldCount;i++) 
    { 
      string fieldName = reader.GetName(i); 
      // use your display method here
      Console.Write(fieldName+"\t"); 
    }         
}
Brad Bruce
  • 7,638
  • 3
  • 39
  • 60