user6363065 user6363065 - 9 days ago 5
C# Question

Firebird dataReader

I would like to see all of the data with column names in my logfile.

private static void ExecuteSQL()
{

string conn = "User ID=SYSDBA;Password=masterkey;Database=XX.18.137.XXX:C:/ER.TDB;DataSource==XX.18.137.XXX;Charset=NONE;";

FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "SELECT * FROM RDB$RELATIONS";

FbCommand myCommand = new FbCommand(sql, myConnection);
try
{
myConnection.Open();
myCommand.CommandTimeout = 0;
myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
// Log.WriteLog(myReader["rdb$relation_name"].ToString());

}
myConnection.Close();
}
catch (Exception e)
{
Log.WriteLog(e.ToString());
}
}


Right now it's only showing me the
rdb$relation_name
column.

I want to check the different tables for which I don't have the column's name.

Answer

All you need to do is iterate over all fields printing their names before iterating the results:

private static void ExecuteSQL()
{

    string conn = "User ID=SYSDBA;Password=masterkey;Database=XX.18.137.XXX:C:/ER.TDB;DataSource==XX.18.137.XXX;Charset=NONE;";

    FbConnection myConnection = new FbConnection(conn);
     FbDataReader myReader = null;
     string sql = "SELECT * FROM RDB$RELATIONS";

    FbCommand myCommand = new FbCommand(sql, myConnection);
    try
    {
        myConnection.Open();
        myCommand.CommandTimeout = 0;
        myReader = myCommand.ExecuteReader();

        // 1. print all field names
        for (int i = 0; i < myReader.FieldCount; i++)
        {
          Log.WriteLog(myReader.GetName(i));
        }

        // 2. print each record
        while (myReader.Read())
        {
          // 3. for each record, print every field value
          for (int i = 0; i < myReader.FieldCount; i++)
          {
            Log.WriteLog(myReader[i].ToString());
          }

        }
        myConnection.Close();
    }
    catch (Exception e)
    {
        Log.WriteLog(e.ToString());
    } 
}

I am pretty sure, that this will give ugly output as it prints every output to a new line. You should be able to change this to print the fields and each record in rows.