如何操纵DateSet里的具体元素? 比如我在一个新的DateSet里充填一个表。我如何用一个字符型变量(比如string query)来返回DateSet里的表的某一行的某一列?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DataSet or DataTable doesn't support string query, use DataTable's Select(..) method or DataView's RowFilter property DataTable.Select 方法获取 DataRow 对象的数组。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 strExpr; string strSort; strExpr = "id > 5"; // Sort descending by column named CompanyName. strSort = "name DESC"; // Use the Select method to find all rows matching the filter. DataRow[] foundRows = customerTable.Select( strExpr, strSort, 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 r in rows ) { foreach( DataColumn c in r.Table.Columns ) { Console.Write( "\t {0}", r[c] ); } Console.WriteLine(); }} 例如:string UID=ds.Tables["tablename"].Rows[rowindex]["uid"].ToString();就是取表tablename里第rowindex行的uid列的值 DataSet ds=new DataSet();//取第一行第一列的值string query=ds.Tables["tablename"].Rows[0][0].ToString(); A 如何得到B中双击的值 "^"这个运算符什么意思 怎么学好C# .NET大文件以二进制上传到数据库并下载问题!!! c#日期格式转换问题 安装C#2008后,怎样使用sql server数据库? C#打印的问题 一个Assembloy.Load的问题,请大家帮帮忙 请教如何在由代码动态生成的datagrid上添加一个checkbox printDocument 旋转180 C# 中的作用有哪些, 一个关于DataGrid的问题
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 strExpr;
string strSort;
strExpr = "id > 5";
// Sort descending by column named CompanyName.
strSort = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select( strExpr, strSort, 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 r in rows )
{
foreach( DataColumn c in r.Table.Columns )
{
Console.Write( "\t {0}", r[c] );
}
Console.WriteLine();
}
}
就是取表tablename里第rowindex行的uid列的值
//取第一行第一列的值
string query=ds.Tables["tablename"].Rows[0][0].ToString();