我想问一下,我可以遍历一个SqlDataReader。并在一行接一行的单个文本框中显示其值?

时间:2018-03-29 17:12:56

标签: c# asp.net sqldatareader

geom_smooth

我的表格名称为protected void Button1_Click(object sender, EventArgs e) { SqlConnection MyConnection = new SqlConnection("Data Source=VIJAYSTIWARI\\SQLEXPRESS;Initial Catalog=earthquake;User ID=sa;Password=HereIsPwd;"); MyConnection.Open(); SqlCommand MyCommand = new SqlCommand("SELECT * FROM eq", MyConnection); SqlDataReader mydr = MyCommand.ExecuteReader(); if (mydr.HasRows) { while(mydr.Read()) { TextBox7.Text = mydr.GetString(1); } } MyConnection.Close(); } ,其中包含以下数据:

eq

1 个答案:

答案 0 :(得分:1)

只需将TextBox7.Text = mydr.GetString(1);更改为:

 TextBox7.Text = TextBox7.Text + "," + mydr.GetString(1);

如果您的文本框支持多行,请执行以下操作:

 TextBox7.Text = TextBox7.Text + Environment.NewLine + mydr.GetString(1);

或创建list,然后您可以使用按钮显示data.Sample:

List<string> countries = new List<string>
......
while(mydr.Read())
         {
             countries.Add(mydr.GetString(1));
 ......

然后在button_Click上,使用:

int myint = 0;
textBox1.Text = countries.Items[myint]
myint = myint + 1;