从数据表中选择特定的行 [英] To select a particular rows from datatable

查看:142
本文介绍了从数据表中选择特定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am的数据表中充满了从数据库中获取的值...(字段为SID,SNAME)

从该数据表中,我只需要选择几行...例如,我需要选择包含(101,102,109)中的SID的行..

建议我使用Sql之类的IN关键字从数据表中获取这些数据...


我不知道如何?帮助我

Am having datatable full of values fetched from database...(the fields are SID,SNAME)

from that datatable i need to select only few rows...for example i need to select the rows containing SID in(101,102,109)..

I was advised to use IN Keyword like Sql to fetch those data from datatable...


I duno how?help me

推荐答案

如何使用IN [ ^ ]

您可能想考虑对回答您问题的人表示赞赏.
How to use IN[^]

And you might want to think about showing some appreciation to those that have answered your questions.


Wes Aday 给IN的参考是很好的.但是,由于在问题中要求从DataTable获取行,所以可以按如下方式使用DataTable Select 方法:
The reference for IN given by Wes Aday is good. But, since it was asked in the question to fetch rows from a DataTable, the Select method of DataTable can be used as follows:
DataRow[] rows = DataTable1.Select(
                    "SID IN (101,102,109)", "", 
System.Data.DataViewRowState.CurrentRows);


Select 方法中可以使用的过滤器表达式在此处说明
http://msdn.microsoft.com/en-us/library/system. data.datacolumn.expression.aspx [ ^ ]
如果要求返回的行是sorted,则可以使用Select 方法的第二个参数,如此处所述
http://msdn.microsoft.com/en-us/library/b5c0xc84.aspx [ ^ ]
System.Data.DataViewRowState.CurrentRows枚举值用作上述Select 方法中的第三个参数,以避免使用Row.Delete 方法返回DataTable 中的行deleted .


The filter expressions which can be used in the Select method are explained here
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx[^]
If the rows returned are required to be sorted, then second parameter of the Select method can be used as explained here
http://msdn.microsoft.com/en-us/library/b5c0xc84.aspx[^]
The System.Data.DataViewRowState.CurrentRows enumeration value is used as the third parameter in the above Select method to avoid returning the rows deleted in the DataTable using Row.Delete method.



试试这个
Hi ,
try this
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
        string statment = "select item_code, Item_name, brand, size, section, price, Material, Qty, tax, tt  from Items ";
        SqlDataAdapter da = new SqlDataAdapter(statment, con);       
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataRow[] foundRows = ds.Tables[0].Select("item_code = 1", " item_code DESC", DataViewRowState.CurrentRows);
        //this is for create datatable with same schema like the orginal one .
        string statment2 = "select item_code, Item_name, brand, size, section, price, Material, Qty, tax, tt  from Items where  item_code = 0";
        SqlDataAdapter da2 = new SqlDataAdapter(statment2, con);
        DataTable dt = new DataTable();
 
        da2.Fill(dt);
        DataRow dr = dt.NewRow();   
        foreach (DataRow row in foundRows)
        {            
            dr[0] = row[0];
            dr[1] = row[1];
            dr[2] = row[2];
            dr[3] = row[3];
            dr[4] = row[4];
            dr[5] = row[5];
            dt.Rows.Add(dr);
        }
        con.Close();
        cmd.Dispose();
        GridView1.DataSource = dt; 
        GridView1.DataBind();
    }



最好的问候
米特瓦里(M.Mitwalli)



Best Regards
M.Mitwalli


这篇关于从数据表中选择特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆