How can i iterate through row of particular table

2019-05-31 09:57发布

问题:

I want to cange the lables in my form according to particular value in rows. I just need how to iterate through table rows.

回答1:

You can use SqlDataSource's Select() method to retrieve data and then you can convert the result into a DataView or a DataReader.

    // Use the result as a DataView.
    var dataview = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    foreach (DataRowView dataviewrow in dataview)
    {
        Label1.Text = dataviewrow["FirstName"].ToString();
    }

    // Use the result as a DataReader.
    var datareader = (SqlDataReader)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
    while (datareader.Read())
    {
        Label2.Text = datareader["LastName"].ToString();

    }
    datareader.Close();