我不明白此代码输出的格式 [英] I don't understand formatting from output of this code

查看:31
本文介绍了我不明白此代码输出的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我的一段代码来自我教授提供的示例.

As title states, my piece of code which was from an example provided by my professor.

我的输出是:

MagazineID Title Publisher Price SubscriptionRate 
1 People Times Inc. 4.95 19.95 
2 Car and Driver Hachetter Inc. 3.95 19.99 

代码:

    private void btnShowMags_Click(object sender, EventArgs e)
    {
        // Creating new instance of the DisplayMags form.
        DisplayMags displayMags = new DisplayMags();

        // find the path where the executable resides
        string dbPath = Application.StartupPath;

        // Providing a path to the MS Access file.
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
            + dbPath + @"\..\..\..\..\Magazines.mdb; User Id=admin; Password=";

        // Creating a new connection and assigning it to a variable.
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = connString;

        // Creating a new instance for a command which we will use later.
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;

        // declare and instantiate the command
        OleDbCommand cmdMagazines = new OleDbCommand();
        cmdMagazines.CommandText = "select * from magazine";
        cmdMagazines.Connection = conn;

        OleDbDataReader drMagazines;

        try
        {
            // open the connection
            conn.Open();

            // retrieve data from the data source to the data reader
            drMagazines = cmdMagazines.ExecuteReader();

            if (drMagazines.HasRows)
            {
                // populate the column headings
                for (int i = 0; i < drMagazines.FieldCount; i++)
                    displayMags.txtDisplayMags.Text += drMagazines.GetName(i) + " ";
                displayMags.txtDisplayMags.Text += "\r\n";

                // populate the data by row
                while (drMagazines.Read())
                {
                    for (int i = 0; i < drMagazines.FieldCount; i++)
                        displayMags.txtDisplayMags.Text += drMagazines.GetValue(i) + " ";
                    displayMags.txtDisplayMags.Text += "\r\n";
                }
            }
        }
        catch (Exception ex)
        {
            // Displaying any errors that might have occured.
            MessageBox.Show("Error opening the connection: " + ex.Message + "\r\n");
        }
        finally
        {
            // Closing connection after task was completed.
            conn.Close();
        }

        // Displaying DisplayMags form, assuring that earlier form
        // will not be accessible. Show() let us access all forms.
        displayMags.ShowDialog();
    }

而且,我试图让它看起来像这样:

And, I am trying to make it look like this:

编辑.这项工作,但这是否代表正确的编程实践?

EDIT. This work, but does this represent correct programming practices?

            if (drMagazines.HasRows)
            {
                while (drMagazines.Read())
                {
                    displayMags.txtDisplayMags.Text += "=== Magazine " + 
                        drMagazines.GetValue(0) + " ===" + Environment.NewLine + 
                        drMagazines.GetValue(1) + Environment.NewLine +
                        drMagazines.GetValue(2) + Environment.NewLine +
                        drMagazines.GetValue(3) + Environment.NewLine +
                        drMagazines.GetValue(4) + Environment.NewLine + 
                        Environment.NewLine;
                }
            }

推荐答案

我不会给你完整的答案,因为那会违背作业的目的,你也学不到任何东西.但是,我可以为您提供有关从哪里开始解决问题的想法.

I'm not giving you the entire answer, because that would defeat the purpose of the assignment and you wouldn't learn anything. However, I can give you ideas on where to start to solve the problem.

您的输出在以 if (drMagazines.HasRows) 开头的块中开始.该块中的代码是您需要进行更改的地方.

Your output starts in the block starting with if (drMagazines.HasRows). The code within that block is where you need to make your changes.

您需要更改它,而不是打印列标题和每行的内容,而是打印一个分隔符,其中包括杂志编号、行尾 (Environment.NewLine)然后将具有标题和内容的行分开.

You need to change it so that instead of printing out the column headings and then the content of each row, you print a separator that includes the magazine number, a line ending (Environment.NewLine) and then separate rows that have titles and then content.

您现在在代码中拥有必要的信息 - 您在填充列标题然后按行填充数据的地方有注释.更改它,以便您填充单个标题,然后填充该标题的行内容.一个建议是将其从两个循环更改为一个循环 - 读取列标题,然后读取它包含的行内容.您可以在该循环期间为每个项目添加任何其他内容或格式.

You have the necessary information in your code now - you have comments where you populate the column headings and then populate the data by row. Change that so that you populate a single heading and then populate the row content for that heading. A suggestion would be to change it from two loops to one - read a column heading, and then the row content that it contains. You can add any additional content or formatting during that loop for each item.

这篇关于我不明白此代码输出的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆