火鸟数据阅读器

Firebird dataReader

我想在我的日志文件中查看所有带有列名的数据。

 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());
        } 
    }

现在它只显示 rdb$relation_name 列。

我想检查我没有列名的不同表。

您需要做的就是在迭代结果之前迭代打印其名称的所有字段:

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());
    } 
}

我很确定,这会产生丑陋的输出,因为它将每个输出打印到一个新行。您应该能够更改它以打印字段和行中的每条记录。

public static List<string> GetColumnNames(string queryString)
    {
        string result = string.Empty;
        List<string> listOfColumns = new List<string>();

        try
        {
            using (FbConnection conn = new FbConnection(connString))
            {
                conn.Open();

                using (FbCommand cmd = new FbCommand(queryString, conn))
                {
                    // Call Read before accessing data.
                    FbDataReader reader = cmd.ExecuteReader();

                    if (reader.FieldCount > 0)
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            listOfColumns.Add(reader.GetName(i));
                        }
                    }
                }
            }
        }

        catch (Exception e)
        {
            BinwatchLogging.Log(e);
        }

        return listOfColumns;

       // return result;
    }

其中 querystring 是您的查询(例如:select * 来自您的表名),connstring 是您的 firebird 连接字符串