从数据表中选择特定值-C#
我有一个表。从该表中,我根据表的关键字段选择行。这次我根据关键“搜索”从数据库中选择行。所以我在数据表中得到了以下行。
ID |关键|价值|指定值
1 次搜索次数 10
4 搜索姓名约翰
6个搜索渠道20
我声明了 3 个变量,
int count,channels;
string name;
我想为该表中的上述变量分配相应的值。
即,我必须从数据表中获取值,如下所示,
count=10,
channels= 20
name=John
我该怎么做?
I have a table.From that table i am selecting rows Based on the key field of the table .This time I am selecting rows from database based on key 'search'.So i got following rows in my datatable .
Id | key | Value | assignedvalue
1 search count 10
4 search name John
6 search channels 20
I have 3 variables declared
int count,channels;
string name;
I want to assign corresponding values to the above variables from that table.
ie, i must get values from data table like shown below
count=10,
channels= 20
name=John
How can i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完成您想做的事情的一种方法是使用 DataTable.Select 方法。
使用此方法,您可以定义一个类似选择的查询并将其应用到您的数据表。您将返回与您的查询匹配的 DataRow 对象数组。下面是改编自我链接的 MSDN 文章中的示例的示例:
注意:我假设带有名称的 DataColumn 称为 NameColumn,而包含要检索的值的列称为 ValueColumn。
不过,如果您确实想使用 LINQ,也可以。请参阅数据表上的 LINQ 查询。
One way to do what you want to do is by using the DataTable.Select method.
Using this method you'd define a select-like query and apply it to your DataTable. You'd get back an array of DataRow objects that match your query. Here's an example adapted from the example in the MSDN article I linked:
Note: I'm assuming the DataColumn with names is called NameColumn, and the column with the values you want to retreive is called ValueColumn.
If you do want to use LINQ however, you can. See LINQ query on a DataTable.
“搜索次数 10”是什么意思?如果您想知道有多少行,
dt.Rows.Count
可以完成这项工作。如果列名称是 FirstName,则可以使用
dr["FirstName"]
获取值。您可以循环并检查该值。“搜索全部选定”是什么意思?如果你想找到“selected”列中值为
true
的行,可以检查if ((dr["selected"] as bool?? ?? false) == true )
尝试改进你的问题。解释你想要实现什么。如果您能提供表的架构,那就太好了。否则“搜索全部选定”之类的事情就没有意义。
What do you mean "search count 10"? if you want to know how many rows you have
dt.Rows.Count
does the job.if the column name is FirstName then
dr["FirstName"]
can be used to get the value. You can loop through and check the value.What do you mean "search selected All"? If you want to find a row with value in "selected" column to be
true
, you can checkif ((dr["selected"] as bool? ?? false) == true)
Try to improve your question. Explain what you want to achieve. It would be good if you could provide the schema of the table. Otherwise things like "search selected All" doesn't make sense.
使用 LINQ to DataSet 可以提供更好的类型安全性、更好的编译时检查,并避免解析筛选表达式的开销。您还可以利用 LINQ 的延迟执行来避免重写每个分配的查询。您可能会考虑这样的事情:
在上面的示例中,我们将 valueName 变量的值更改为我们在 DataTable 中查找的值的名称,然后通过调用 FirstOrDefault 强制查询重新枚举。
Using LINQ to DataSet gives you better type safety, better compile time checking, and avoids the overhead of parsing the filter expression. You can also take advantage of LINQ's deferred execution to avoid rewriting the query for each of the assignments. You may consider something like this:
In the above sample we change the value of the valueName variable to the name of the value we're looking for in the DataTable then force the query to re-enumerate with the call to FirstOrDefault.