任何人都有比这更容易的方法?

时间:2021-04-25 19:55:24

I have this code and in reader. How can I get reader.Read without if methods?

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

        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;

    }

Thanks for your advice. I use dynamic textbox that add automatically if I click the button.

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

i want if cleft=1 then reader.read()=1, but i duuno if cleft=10? that must be i must write if condition until 10 times? its too long ...

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

1 个解决方案

#1


If you are just looking to get rid of the If statements you could use a For instead.

如果你只是想摆脱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;

#1


If you are just looking to get rid of the If statements you could use a For instead.

如果你只是想摆脱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;