gpt4 book ai didi

C#:使用 PrimaryKey 从 DataTable 中检索值

转载 作者:太空狗 更新时间:2023-10-29 22:53:44 24 4
gpt4 key购买 nike

我的 C# 代码有问题。我已经设置了几个数据表,每个数据表都分配了一个主键。我想要做的是从单个列中检索单个行。

假设我有这段代码:

DataColumn Pcolumn = new DataColumn();
DataColumn[] key = new DataColumn[1];

Pcolumn.DataType = System.Type.GetType("System.Double");

Pcolumn.ColumnName = "length";
key[0] = Pcolumn;


table6F.Columns.Add(Pcolumn);
table6F.Columns.Add("Area", typeof(double));
table6F.Columns.Add("load", typeof(double));
table6F.Columns.Add("weigth", typeof(double));

table6F.PrimaryKey = key;
table.Rows.Add(6.0, 14.0, 17.8 , 11.0 );
table.Rows.Add(7.0, 16.2 , 20.7 , 16.0 );

我想检索第二行 (20.7) 的“负载”,我想在表中搜索主键列 7.0。我模拟测试这样做,只是为了测试:

Object oV;
double load;

//Get an Table object given the specific row number, this dummy i always set to 0.
// Given Column
oV = table.Rows[0]["load"];
load = Convert.ToDouble(oV.ToString());

有没有类似的方式使用主键提取?

最佳答案

您可以从 DataTable 中检索一行基于其主键使用 DataRowCollection.Find方法。在您的情况下,它将是:

DataRow matchingRow = table.Rows.Find(7.0D);
double value = (double)matchingRow["load"];

关于C#:使用 PrimaryKey 从 DataTable 中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com