リーダー変数からFieldCountを取得するとします。
ここでは、元のクエリによって取得された各フィールドのテキストボックスを作成して、ループ内でループを実行する必要があります。
最後に、あなたは彼らが他の人の上にそれぞれに作成されるのTextBoxのLocationプロパティを設定しないと、あなただけのループ
// Get the number of fields present in the reader....
int count = reader.FieldCount;
// Read one record at time
while(reader.Read())
{
// Create a textbox for each field in the record
for (int i = 0; i < count; i++)
{
TextBox txt1 = new TextBox();
// Set its location on screen
// Probably if you have many fields you need to
// use a better algorithm to calculate x,y position
txt1.Location = new Point(x, y);
txt1.Text = reader[i].ToString();
txt1.Name = i.ToString();
x = x + 25;
y = y + 25;
panel1.Controls.Add(txt1);
}
}
で得られた最新の値を持つ最上位の1を参照してください場合フィールドのテキストボックスのみを作成する場合は、sikicerikの2つのオプションがあります。最初のものはお勧め、それはあなたのデータベース上の少ないオーバーヘッドが発生するためには
SELECT sikicerik FROM yourTableName
に対してリードを構築するために使用するSELECTクエリを変更することで構成されている他のオプションは、あなたのコードに簡単な変更です。 1つのフィールドだけに興味があることを既に知っているので、FieldCountをループする必要はありません。
// Read one record at time
while(oku.Read())
{
textBox1.Text = oku["soru"].ToString();
label1.Text = Form2.sinavno;
// Create the single textbox required for the only field required
TextBox txt1 = new TextBox();
// Set its location on screen
txt1.Location = new Point(x, y);
txt1.Text = oku["sikicerik"].ToString();
txt1.Name = i.ToString();
x = x + 25;
y = y + 25;
panel1.Controls.Add(txt1);
// Repeat the loop for each record.
}
代わりにそれぞれの後に 'while(reader.Read())'は必要ありません。ループ反復、write 'reader.Read()' – amyn
@amynが試したが、同じ問題が残る –
上記の質問を更新されたコードで更新できますか? – amyn