从 VB.net 中的数据表中选择条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18961607/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:11:38  来源:igfitidea点击:

Select with condition from a datatable in VB.net

sqlvb.netdatatable

提问by HelpASisterOut

I want to select a certain field from a datatable in VB based on the value of another field in the same row.

我想根据同一行中另一个字段的值从 VB 中的数据表中选择某个字段。

In SQL, it would easily be done by writing this query:

在 SQL 中,可以通过编写以下查询轻松完成:

select error_message from table_errors where error_case="condition"

How do I do this if I have my SQL table filled in a datatable in VB? How do I select the item("error_message") in the datatable based onthe item("error_Case") field?

如果我在 VB 中的数据表中填充了我的 SQL 表,我该怎么做?如何根据item("error_Case") 字段选择数据表中的 item("error_message") ?

Any help would be appreciated

任何帮助,将不胜感激

回答by Tim Schmelter

You can use Linq-To-DataSet:

您可以使用Linq-To-DataSet

Dim matchingRows As IEnumerable(Of DataRow) = 
    From row In table
    Where row.Field(Of String)("error_case") = "condition"

If you just want one column (of course that works also in one step):

如果您只想要一列(当然也可以在一个步骤中使用):

Dim errorMessages As IEnumerable(Of String) = 
    From row In matchingRows 
    Select row.Field(Of String)("error_message")

For Each error In errorMessages 
    Console.WriteLine(error)
Next 

If you expect it to be just a single row use Firstor Single(throws an exception if there is more than one row):

如果您希望它只是一行,请使用Firstor Single(如果有多行,则抛出异常):

Dim error As String = errorMessages.First()

Since Firstthrows an exception if the sequence is empty you can use FirstOrDefault:

由于First如果序列为空会引发异常,您可以使用FirstOrDefault

Dim error As String = errorMessages.FirstOrDefault() ' is null/Nothing in case of an empty sequence 

All in one line (note that both Linq and DataTable.Selectneeds to use loops):

全部在一行中(请注意,Linq 和都DataTable.Select需要使用循环):

Dim ErrMessage As String = errorTable.AsEnumerable().
    Where(Function(r) r.Field(Of String)("Error_Case") = TextCase.Text).
    Select(Function(r) r.Field(Of String)("Error_Message")).
    FirstOrDefault()

回答by Catalin

here is a worker version of the rough code

这是粗略代码的工人版本

    Dim connString As String = "select error_message from table_errors where error_case='condition'"
    Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(connString)
    conn.Open()
    Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(connString, conn)
    Dim dTable As DataTable = New DataTable()
    Dim dAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd)
    dAdapter.Fill(dTable)
    conn.Close()
    Dim text As String
    Dim dReader As DataTableReader = dTable.CreateDataReader()
    While dReader.Read()
        text = dReader.GetValue(0)
    End While
    dReader.Close()