c#中,DataTable.Select()中括号里的参数的语法规则,是个什么样的? 本人新手,请教
展开全部
Select(String)/Select(String, String)/Select(String, String, DataViewRowState)
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();
}
}
自己看看文档
http://technet.microsoft.com/zh-cn/library/system.data.datatable.select(v=vs.110)
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();
}
}
自己看看文档
http://technet.microsoft.com/zh-cn/library/system.data.datatable.select(v=vs.110)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataTable.Select() 括号里的为一个条件, 类似SQL的 where 条件
比如 DataTable中有字段A 查询 字段A等1的数据 可以用 DataTable.Select("A=1")
比如 DataTable中有字段A 查询 字段A等1的数据 可以用 DataTable.Select("A=1")
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1、select(string):
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]);
}
}
2、select(string,string)
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 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]);
}
}
2、select(string,string)
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]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
vs 不是有提示吗 要么不加参数 要么加string 类型的参数
string 有什么语法规则啊
string 有什么语法规则啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询