如何仅从一列获取一行数据 [英] How to get one row data from one column only
问题描述
当我写作
foreach(dt.Rows中的DataRow行)
{
html .Append();
foreach(dt.Columns中的DataColumn列)
{
html.Append();
}
html.Append(
); html.Append(row [column.ColumnName]); html.Append( | ); string file = row [EmailID]。ToString(); i我收到所有用户的所有电子邮件ID。我想只有一个用户的电子邮件ID。我怎么能得到这个? |
when i am writing
foreach (DataRow row in dt.Rows)
{
html.Append("");
foreach (DataColumn column in dt.Columns)
{
html.Append("");
}
html.Append("
"); html.Append(row[column.ColumnName]); html.Append(" | "); string file = row["EmailID"].ToString(); i am getting all email ids of all users. I want email id of only one user. How can I get this?? |
推荐答案
有几种方法,但我' d首先检索您感兴趣的用户的信息。而不是SELECT查询是一般的:
There are a couple of ways, but I'd start by retrieving only the info the one user you are interested in. Instead of your SELECT query being general:
SELECT * FROM MyTable
添加WHERE子句:
Add a WHERE clause:
SELECT * FROM MyTable WHERE userID = 12345
这样,你没有得到你不打算使用的信息(效率更高)而且不必根本使用循环(甚至是DataTable和DataAdapter - 如果你想要的只是电子邮件ID,那么你可以指定列名,并使用ExecuteScalar返回单个值。)
That way, you aren't getting info you don't intend to use (which is a lot more efficient) and don't have to use a loop at all (or even a DataTable and DataAdapter - if all you want is the email id, then you can specify the column name, and use ExecuteScalar to return a single value).
这篇关于如何仅从一列获取一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!