任何人都比这方法容易吗?

时间:2015-05-12 07:36:06

标签: c# ms-access

我有这个代码和读者。 我怎样才能获得读者。如果没有方法,请不要?

        System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string query = " select * FROM Other ORDER BY Type";
        command.CommandText = query;
        OleDbDataReader reader = command.ExecuteReader();


        txt.Text = "txtpertanyaan"+cLeft;
        this.Controls.Add(txt);
        txt.Top = cLeft *25;
        txt.Left = 100;

        if (cLeft == 1)
        {
            if (reader.HasRows)
            {

                reader.Read();

                txt.Text = reader["Type"].ToString();
                reader.Read();

            }
        }
        else if (cLeft ==2)
        {
            if (reader.HasRows)
            {

                reader.Read();
                reader.Read();
                txt.Text = reader["Type"].ToString();
                reader.Read();

            }
        }
        else if (cLeft == 3)
        {
            if (reader.HasRows)
            {

                reader.Read();
                reader.Read();
                reader.Read();
                txt.Text = reader["Type"].ToString();
                reader.Read();

            }
        }

        cLeft = cLeft+1;
        return txt;

    }

感谢您的建议。我使用动态文本框,如果单击按钮,则会自动添加。

我想要如果cleft = 1那么reader.read()= 1,但如果cleft = 10,我想duuno?必须是我必须写,如果条件直到10次?它太长了......

1 个答案:

答案 0 :(得分:0)

如果您只想摆脱If语句,可以改用For

System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = " select * FROM Other ORDER BY Type";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();

txt.Text = "txtpertanyaan"+cLeft;
this.Controls.Add(txt);
txt.Top = cLeft *25;
txt.Left = 100;

if (reader.HasRows)
{
    for (int i = 0; i < cLeft ; i++)
    {
        reader.Read();
    }
    txt.Text = reader["Type"].ToString();
    Reader.Read();
}    

cLeft = cLeft+1;
return txt;
相关问题