从DataTable读取值

时间:2012-07-19 08:36:01

标签: c# datatable

我有一个填充了samo数据/值的DataTable,我想从DataTable中读取数据并将其传递给字符串变量。

我有这段代码:

DataTable dr_art_line_2 = ds.Tables["QuantityInIssueUnit"];

我有这样的对手:

for (int i = 1; i <= broj_ds; i++ )
{                                    
    QuantityInIssueUnit_value => VALUE FROM DataTable
    QuantityInIssueUnit_uom  => VALUE FROM DataTable    
}

这可能与否?如果是,那么如何将数据从DataTable传递给那些变量?

谢谢!

4 个答案:

答案 0 :(得分:19)

DataTable dr_art_line_2 = ds.Tables["QuantityInIssueUnit"];

for (int i = 0; i < dr_art_line_2.Rows.Count; i++)
{
    QuantityInIssueUnit_value = Convert.ToInt32(dr_art_line_2.Rows[i]["columnname"]);
    //Similarly for QuantityInIssueUnit_uom.
}

答案 1 :(得分:6)

您可以使用foreach循环

来完成
DataTable dr_art_line_2 = ds.Tables["QuantityInIssueUnit"];

  foreach(DataRow row in dr_art_line_2.Rows)
  {
     QuantityInIssueUnit_value = Convert.ToInt32(row["columnname"]);
  }

答案 2 :(得分:0)

我认为它会起作用

for (int i = 1; i <= broj_ds; i++ ) 
  { 

     QuantityInIssueUnit_value = dr_art_line_2[i]["Column"];
     QuantityInIssueUnit_uom  = dr_art_line_2[i]["Column"];

  }

答案 3 :(得分:0)

对于 VB.Net 是

        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "database path")
        Dim cmd As New OleDb.OleDbCommand
        Dim dt As New DataTable
        Dim da As New OleDb.OleDbDataAdapter
        con.Open()
        cmd.Connection = con
        cmd.CommandText = sql
        da.SelectCommand = cmd

        da.Fill(dt)

        For i As Integer = 0 To dt.Rows.Count
            someVar = dt.Rows(i)("fieldName")
        Next