一种方法是将初始查询结果加载到 a 中DataTable
,然后使用该.Select
方法针对初始结果运行辅助查询。例如,对于 Access 中名为 [Users] 的表,包含...
ID UserName
-- --------
1 Jeff
2 Greg
3 Garth
4 Gord
5 Gary
6 Grant
7 Graham
...以下 C# 代码...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace oledbTest1
{
class Program
{
static void Main(string[] args)
{
using (var conn = new OleDbConnection())
{
conn.ConnectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=C:\__tmp\testData.accdb;";
conn.Open();
using (var cmd = new OleDbCommand())
{
cmd.Connection = conn;
cmd.CommandText =
"SELECT * FROM Users WHERE ID < 7 ORDER BY UserName";
var da = new OleDbDataAdapter(cmd);
var dt = new System.Data.DataTable();
da.Fill(dt);
Console.WriteLine("The initial query from the Access database (WHERE ID < 7) returned:");
foreach (System.Data.DataRow dr in dt.Rows)
{
Console.WriteLine(dr["UserName"]);
}
System.Data.DataRow[] subsetRows;
subsetRows = dt.Select("UserName LIKE 'Gr%'");
Console.WriteLine();
Console.WriteLine("The equivalent of \".FindFirst UserName LIKE 'Gr%'\" on that subset would be:");
Console.WriteLine(subsetRows[0]["UserName"]);
}
conn.Close();
}
}
}
}
...产生以下结果:
The initial query from the Access database (WHERE ID < 7) returned:
Garth
Gary
Gord
Grant
Greg
Jeff
The equivalent of ".FindFirst UserName LIKE 'Gr%'" on that subset would be:
Grant