vb.net DataTable Select(String) 函数帮助 VB .NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6427130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTable Select(String) Function Help VB .NET
提问by gblock
I made a datatable with 2 columns a transactionTime column and a numberOfTransactions column. I made the table with the pre-defined transaction times and want to add the number of transactions from an XML file. I have gotten through the XML file and want to add the data to the correct row. Here is the function:
我制作了一个包含 2 列的数据表,一个 transactionTime 列和一个 numberOfTransactions 列。我使用预定义的事务时间制作了表格,并希望从 XML 文件添加事务数。我已经通过 XML 文件并希望将数据添加到正确的行。这是函数:
Function AddRow(ByVal timeOfTransaction As String, ByVal numberOfTransactions As String, ByRef dataTableOfTransactions As DataTable) As String
Dim row() As DataRow = dataTableOfTransactions.Select("transactionTime = timeOfTransaction")
If row(0) IsNot Nothing Then
row(0)("numberOfTransactions") = numberOfTransactions
End If
Return Nothing
End Function
When I run this it overwrites the first element in the table's numberOfTransactions coloumn. I know it has to do with the "transactionTime = timeOfTransaction" part but I can't seem to get it to read timeOfTransaction as a reference to a string instead of a literal. Any help would be much appreciated. Thanks!
当我运行它时,它会覆盖表的 numberOfTransactions 列中的第一个元素。我知道它与“transactionTime = timeOfTransaction”部分有关,但我似乎无法让它读取 timeOfTransaction 作为对字符串而不是文字的引用。任何帮助将非常感激。谢谢!
回答by Joss57
You need to write something like this :
你需要写这样的东西:
Dim row() As DataRow = dataTableOfTransactions.Select("transactionTime=#" & timeOfTransaction & "#")
But be careful with your date/month or month/date format, it depends of your regional settings.
但是请注意您的日期/月份或月份/日期格式,这取决于您的区域设置。
回答by msarchet
row(0)("numberOfTransactions") = numberOfTransactions
Right there you are telling the program to overwrite that value with number of transactions.
就在那里,您告诉程序用事务数覆盖该值。
If you want that value you need to set it to something, not set something to it.
如果您想要该值,则需要将其设置为某个值,而不是为其设置某个值。
Also, if you want your select to work properly try doing it like this
另外,如果您希望您的选择正常工作,请尝试这样做
dataTableOfTransactions.Select("transactionTime = " + timeOfTransaction)