更新内存数据表中的单个列 [英] Update a single column in a in-memory datatable

查看:79
本文介绍了更新内存数据表中的单个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内存数据表,它有很多列,我从SQL数据库的不同表中绘制。我可以通过内部或左侧连接来实现,但是当有人不想要所有列时,开销是巨大的,所以我使用的是数据表(不涉及数据集)。所有工作都很精彩,直到我想要更新其中一个列。



我已经搜索了关键字以解决我的问题以及查看代码项目但我无法看到我的内容之后(除非它被埋在其他类似的问题中)。



我正在使用的代码如下,借用微软的网站,尽管它确实希望它有一个数据集跳过那部分,IDE说实际运行之前没有任何问题。



我尝试了什么:



 Dim MyRow()As Data.DataRow 
MyRow = Facility_Table.Select(FacilityNameRef = 123)
MyRow(0) (已锁定)= 0





(此处放置的引用只是一个虚拟但我知道引用存在于一旦填充了数据表,我就可以从datagridview看到我已绑定到数据表。

解决方案

我想你需要说

 MyRow(0).Item(L ocked)= 0 


Quote:

更新单个列内存数据表

...

  Dim  MyRow () As  Data.DataRow 
MyRow = Facility_Table。选择 FacilityNameRef = 123
MyRow( 0 ) ( 已锁定)= 0







嗯...

你不能以这种方式更新单列(该列中的所有行),因为 DataTable.Select方法 [ ^ ]返回行数组。你必须遍历集合中的行才能更改它们的值。



  Dim  MyRows() As  Data.DataRow = Facility_Table。选择  FacilityNameRef = 123
对于 每个 r 作为 Data.DataRow MyRows
r( ColumnName)= 0
'
' r.Item(ColumnName)= 0
下一页


I have an in-memory datatable that has lots of columns that I am drawing in from different tables in a SQL database. I could do it via inner or left joins but the overhead is tremendous when someone doesnt want all of the columns so I am using a datatable (no dataset involved). All works wonderfully until I want to update one of the columns.

I have googled keywords for my question as well as looked at code project but I cant see what I am after (unless it is buried among other similar questions).

The code I am using is below, borrowed from Microsoft's website, though it does expect it to have a dataset, skipping that part, the IDE is saying nothing is wrong until I actually run it.

What I have tried:

Dim MyRow() As Data.DataRow
               MyRow = Facility_Table.Select("FacilityNameRef=123")
               MyRow(0)("Locked") = 0



(the reference put here is just a dummy but I know the reference exists in the datatable once it is populated and I can see that from a datagridview that I have bound to the datatable.

解决方案

I think you need to say

MyRow(0).Item("Locked") = 0


Quote:

Update a single column in a in-memory datatable
...

Dim MyRow() As Data.DataRow
MyRow = Facility_Table.Select("FacilityNameRef=123")
MyRow(0)("Locked") = 0




Well...
You can't update single column (all rows in that column) this way, because DataTable.Select method[^] returns an array of rows. You have to loop through the rows in collection to be able to change their values.

Dim MyRows() As Data.DataRow = Facility_Table.Select("FacilityNameRef=123")
For Each r As Data.DataRow in MyRows
    r("ColumnName") = 0
    'or
    'r.Item("ColumnName") = 0
Next


这篇关于更新内存数据表中的单个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆