0

I want to output to my console a nicely formatted table with data from SqlDataReader.

I found this answer here on SO with a nice class to do all the work, however, I need some help to implement the SqlDataReader part.

My code for printing out the table looks like this:

        SqlDataReader data = getCommentsFromDB();
        int value = 997;
        string[,] arrValues = new string[5, 5];
        for (int i = 0; i < arrValues.GetLength(0); i++)
        {
            for (int j = 0; j < arrValues.GetLength(1); j++)
            {
                value++;
                arrValues[i, j] = value.ToString();
            }
        }
        ArrayPrinter.PrintToConsole(arrValues);
        Console.ReadLine();

getCommentsFromDB looks like this:

        SqlConnection conn = dal.connectDatabase();
        conn.Open();
        cmd = new SqlCommand(@"SELECT * FROM GuestBook", conn);
        rdr = cmd.ExecuteReader();
        return rdr;

If you need anything else, please tell.

UPDATE

I got it a bit further. However, now I am getting this nasty error:

Error: {0}System.NullReferenceException: Object reference not set to an instance of an object.
   at GuestBook.ArrayPrinter.GetMaxCellWidth(String[,] arrValues) in \GuestBook\GuestBook\ArrayPrinter.cs:line 39
   at GuestBook.ArrayPrinter.GetDataInTableFormat(String[,] arrValues) in \GuestBook\GuestBook\ArrayPrinter.cs:line 60
   at GuestBook.ArrayPrinter.PrintToConsole(String[,] arrValues) in \GuestBook\GuestBook\ArrayPrinter.cs:line 117
   at GuestBook.StudentManager.showAllComments() in \GuestBook\GuestBook\StudentManager.cs:line 49
   at GuestBook.ConsoleGUI.start(String[] args) in \GuestBook\GuestBook\ConsoleGUI.cs:line 28
   at GuestBook.Program.Main(String[] args) in \GuestBook\GuestBook\Program.cs:line 20

With my experience, I would say something is wrong with the class I am using. Maybe it needs an update?

I am running in VS2012 and C#.NET 4.0

Update 2

My data prints out like this:

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|       X         |                 |                 |                 |
|                 |        X        |                 |                 |
|                 |                 |       X         |                 |
|                 |                 |                 |        X        |
-------------------------------------------------------------------------

and not in a single row.

My code so far:

    public void showAllComments()
    {
        SqlDataReader reader = getCommentsFromDB();

        string[,] arrValues = new string[5, 3];

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (!reader.Read()) break; //no more rows
                {
                    arrValues[i, j] = reader[j].ToString();
                }
            }
        }
        ArrayPrinter.PrintToConsole(arrValues);
    }

Also, I would like it to expand vertically to contain all the data in the database.

4

1 に答える 1

1

配列プリンター クラスをSqlDataReaderと連携させるには、 DataReaderに含まれるデータを文字列の配列に移動する必要があります。

Reader を次の行に移動するRead()関数を条件として使用する while ループを使用して、 SqlDataReaderにアクセスできます。[]演算子を使用して、列の位置またはフィールド名をインデックスとして値にアクセスできます。

while (reader.Read())
{
    string myVal = reader["COLUMN_NAME"].ToString();
}

リーダーの最初の 5 行の最初の 5 列を読みたいとすると、for ループを使用してこのようなことを行うことができます (明らかに、コードをより柔軟にする必要があります)。

編集:修正されたコード

SqlDataReader reader = command.ExecuteReader();

string[,] arrValues = new string[5, 5];

for (int i = 0; i < 5; i++)
{
    if (!reader.Read()) break; //no more rows 
    for (int j = 0; j < 5; j++)
    {
        arrValues[i,j] = reader[j].ToString();
    }
}

編集2:

これにより、行の問題の乱数が修正されます。最適な解決策ではありませんが、うまくいくはずです。 警告テキストエディタで書いたばかりで、コンパイラでテストできませんでした

public void showAllComments()
    {
        SqlDataReader reader = getCommentsFromDB();
        List<List<string>> myData; //create list of list
        int fieldN = reader.FieladCount; //i assume every row in the reader has the same number of field of the first row
        //Cannot get number of rows in a DataReader so i fill a list
        while (reader.Read())
        {
            //create list for the row
            List<string> myRow = new List<string>();
            myData.Add(myRow);//add the row to the list of rows
            for (int i =0; i < fieldN; i++)
            {
                myRow.Add(reader[i].ToString();//fill the row with field data
            }
        }

        string[,] arrValues = new string[myData.Count, fieldN]; //create the array for the print class

        //go through the list and convert to an array
        //this could probably be improved 
        for (int i = 0; i < myData.Count; i++)
        {
            List<string> myRow = myData[i];//get the list for the row
            for (int j = 0; j < nField; j++)
            {
                arrValues[i, j] = myRow[j]; //read the field
            }
        }
        ArrayPrinter.PrintToConsole(arrValues);
    }
于 2012-10-24T13:02:40.967 に答える