Looping through a DataTable

前端 未结 4 807
無奈伤痛
無奈伤痛 2021-02-07 03:51

Well. I have a DataTable with multiple columns and multiple rows.

I want to loop through the DataTable dynamically basically the output should look as follows excluding

4条回答
  •  后悔当初
    2021-02-07 04:18

    Please try the following code below:

    //Here I am using a reader object to fetch data from database, along with sqlcommand onject (cmd).
    //Once the data is loaded to the Datatable object (datatable) you can loop through it using the datatable.rows.count prop.
    
    using (reader = cmd.ExecuteReader())
    {
    // Load the Data table object
      dataTable.Load(reader);
      if (dataTable.Rows.Count > 0)
      {
        DataColumn col = dataTable.Columns["YourColumnName"];  
        foreach (DataRow row in dataTable.Rows)
        {                                   
           strJsonData = row[col].ToString();
        }
      }
    }
    

提交回复
热议问题