我有一个大表,其中有一个ID列,其中包含请求/票证信息。
在胜利表格上,我想让用户输入他的请求ID并以新形式获取所有请求信息。
我面临的问题是如何遍历仅一列的值。
这是我在其他地方看到并尝试实现的内容(我的数据表名称为st):
DataTable dt = new DataTable("dbo.table_submission");
DataRow[] foundRows = dt.Select("ID =" + i);
if (foundRows != null )
{
Status status = new Status();
status.Show();
}
DataTable有一个Select方法,该方法可以为您检索匹配的行,而无需使用显式循环。
例如,假设您要搜索的值在名为的列中 RequestID
int yourValueToSearch = 1;
DataRows[] foundRows = dt.Select("RequestID = " + yourValueToSearch);
if(foundRows != null && foundRows.Length > 0)
{
DataTable result = foundRows.CopyToDataTable();
// Pass the result table to your form....
}
这也可能是字符串,但您需要添加适当的引号
string yourValueToSearch = "ABC123";
DataRows[] foundRows = dt.Select("RequestID = '" + yourValueToSearch + "'");
最终您也可以使用Linq
var foundRows = dt.AsEnumerable()
.Where(x => x.Field<int>("RequestID") == yourValueToSearch)
.ToArray()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句