DataTable的Select方法
2014-11-21 10:28
357 查看
DataTable的Select方法
获取 DataRow 对象的数组。
重载列表
名称 说明
Select() 获取所有 DataRow 对象的数组。
Select(String)
按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。
Select(String, String)
获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。
Select(String, String, DataViewRowState)
获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。
示例:
private void GetRows()
{
// Get the DataTable of a DataSet.
DataTable table = DataSet1.Tables["Suppliers"];
DataRow[] rows = table.Select();
// Print the value one column of each DataRow.
for(int i = 0; i < rows.Length ; i++)
{
Console.WriteLine(rows[i]["CompanyName"]);
}
}
//**********************************************************************************************************
//**********************************************************************************************************
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression = "Date > '1/1/00'";
// Sort descending by column named CompanyName.
string sortOrder = "CompanyName DESC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
//**********************************************************************************************************
private static void GetRowsByFilter()
{
DataTable customerTable = new DataTable("Customers");
// Add columns
customerTable.Columns.Add("id", typeof(int));
customerTable.Columns.Add("name", typeof(string));
// Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[]
{ customerTable.Columns["id"] };
// Add ten rows
for(int id=1; id<=10; id++)
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) });
}
customerTable.AcceptChanges();
// Add another ten rows
for(int id=11; id<=20; id++) { customerTable.Rows.Add( new object[] { id, string.Format("customer{0}",
id) });
}
string expression;
string sortOrder;
expression = "id > 5";
// Sort descending by column named CompanyName.
sortOrder = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select(expression, sortOrder,
DataViewRowState.Added);
PrintRows(foundRows, "filtered rows");
foundRows = customerTable.Select();
PrintRows(foundRows, "all rows");
}
private static void PrintRows(DataRow[] rows, string label)
{
Console.WriteLine("\n{0}", label);
if(rows.Length <= 0)
{
Console.WriteLine("no rows found");
return;
}
foreach(DataRow row in rows)
{
foreach(DataColumn column in row.Table.Columns)
{
Console.Write("\table {0}", row[column]);
}
Console.WriteLine();
}
}
获取 DataRow 对象的数组。
重载列表
名称 说明
Select() 获取所有 DataRow 对象的数组。
Select(String)
按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。
Select(String, String)
获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。
Select(String, String, DataViewRowState)
获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。
示例:
private void GetRows()
{
// Get the DataTable of a DataSet.
DataTable table = DataSet1.Tables["Suppliers"];
DataRow[] rows = table.Select();
// Print the value one column of each DataRow.
for(int i = 0; i < rows.Length ; i++)
{
Console.WriteLine(rows[i]["CompanyName"]);
}
}
//**********************************************************************************************************
private void GetRowsByFilter(){ DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string expression; expression = "Date > #1/1/00#"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++) { Console.WriteLine(foundRows[i][0]); }}
//**********************************************************************************************************
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression = "Date > '1/1/00'";
// Sort descending by column named CompanyName.
string sortOrder = "CompanyName DESC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
//**********************************************************************************************************
private static void GetRowsByFilter()
{
DataTable customerTable = new DataTable("Customers");
// Add columns
customerTable.Columns.Add("id", typeof(int));
customerTable.Columns.Add("name", typeof(string));
// Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[]
{ customerTable.Columns["id"] };
// Add ten rows
for(int id=1; id<=10; id++)
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) });
}
customerTable.AcceptChanges();
// Add another ten rows
for(int id=11; id<=20; id++) { customerTable.Rows.Add( new object[] { id, string.Format("customer{0}",
id) });
}
string expression;
string sortOrder;
expression = "id > 5";
// Sort descending by column named CompanyName.
sortOrder = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select(expression, sortOrder,
DataViewRowState.Added);
PrintRows(foundRows, "filtered rows");
foundRows = customerTable.Select();
PrintRows(foundRows, "all rows");
}
private static void PrintRows(DataRow[] rows, string label)
{
Console.WriteLine("\n{0}", label);
if(rows.Length <= 0)
{
Console.WriteLine("no rows found");
return;
}
foreach(DataRow row in rows)
{
foreach(DataColumn column in row.Table.Columns)
{
Console.Write("\table {0}", row[column]);
}
Console.WriteLine();
}
}
相关文章推荐
- 关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
- 在DataTable中执行Select("条件")后,返回DataTable的方法
- 对DataTable进行过滤筛选的一些方法Select,dataview
- DataTable.Select 方法 (String, String, DataViewRowState)
- DataTable.Select() 方法的单引号问题
- DataTable.Select方法的性能问题
- DataTable.Select方法
- 遍历DataTable内存数据的三种方法性能对比 dataTable.Select 用过不?
- DataTable.Select 方法 ()
- DataTable.Select 方法 (String, String)
- 在DataTable中执行Select("条件")后,返回DataTable的方法
- 关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
- 看清DataTable中的Select方法
- 遍历DataTable内存数据的三种方法性能对比 dataTable.Select 用过不?
- 关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法[转载]
- DataTable.Select() 方法的简单用法
- C# DataTable.Select()方法,条件中使用类型转换
- 关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
- 如果表里面有字段parent然后就不能用datatable.select方法估计rowfilter也不能用不知道为什么
- dataTable.Select使用方法