我想问,我可以通过sqldatareader迭代吗?并在单个文本框一行中显示其值


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,其中包含此数据:

country
---------
india
japan
mexico
afghanistan
australia
hungary

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

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

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

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

或创建list,然后您可以使用按钮显示数据。样本:

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;

相关内容

  • 没有找到相关文章

最新更新