Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:使用PrimaryKey从数据表中检索值_C#_Ado.net_Datatable_Primary Key - Fatal编程技术网

C#:使用PrimaryKey从数据表中检索值

C#:使用PrimaryKey从数据表中检索值,c#,ado.net,datatable,primary-key,C#,Ado.net,Datatable,Primary Key,我的C语言代码有问题。我已经设置了两个数据表,每个表都有一个主键。我要做的是从单个列中检索单个行 假设我有以下代码: DataColumn Pcolumn = new DataColumn(); DataColumn[] key = new DataColumn[1]; Pcolumn.DataType = System.Type.GetType("System.Double"); Pcolumn.ColumnName = "length"; key[0] = Pcolumn; tabl

我的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());

是否有类似的方法使用主键进行提取

您可以使用Find方法使用主键查找行

DataRow row = dataTable.Rows.Find("your key");
if(null != row)
{
    string value = row["ColumnName"];
}

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

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

谢谢你们!实施了Enricos解决方案,效果非常好!