10

我有一个填充了 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

4 回答 4

21
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.
}
于 2012-07-19T08:38:59.747 回答
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"]);
  }
于 2012-07-19T08:41:01.473 回答
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"];

  }
于 2012-07-19T08:43:06.720 回答
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
于 2021-01-11T08:41:54.530 回答