客户端有一个DataSet: dsOrders;   dsOrders里面有一个Table: Details,Details(字段如下, Primary key 为OrderID + ProductID):
OrderID     int32   --订单号(主键)
ProductID   int32   --产品编号(主键)
UnitPrice   decimal --单价
Quantity    int32   --数量
Description string  --描述我想实现以下2个语句,1: 当我点Button1按钮的时候,判断dsOrders.Tables["Details"]表中的OrderID == 5 and ProductID == 2 这条记录是否存在???2: 当我点Button2按钮的时候,找出dsOrders.Tables["Details"]表中Description == "China" 共有几条记录???(注: dsOrders.Details的数据由SqlDataAdapter1.Fill(dsOrders, "Details")得到,以上2个问题需要直接在客户端的DataSet里面查到, 不能使用SqlCommand去服务器查),请问怎么写??,非常感谢!

解决方案 »

  1.   

    你的意思就是用DataSet自带的查询功能去查询筛选对吗?
      

  2.   

    无如果你的Sql语句是SELECT * FROM tb
    第二个无法实现
    你的要求是完全只操作内存 不在进行数据库操作
    第一个可以通过new一个DataTable 有个Select 方法
      

  3.   

    DataView dv= dsOrders.Tables["Details"].DefaultView;dv.RowFilter="OrderID = 5 and ProductID = 2";
    MessageBox.Show(dv.Count.ToString());dv.RowFilter="Description ='China'";
    MessageBox.Show(dv.Count.ToString());
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  4.   

    如果直接在数据库里面操作会更加简单
    (1)
    select * from dsOrders where OrderID=5 and ProductID=2
    (2)
    select count(*) from dsOrders where Description="China"
      

  5.   

    DataView dv= dsOrders.Tables["Details"].DefaultView; dv.RowFilter="OrderID = 5 and ProductID = 2"; 
    MessageBox.Show(dv.Count.ToString()); dv.RowFilter="Description ='China'"; 
    MessageBox.Show(dv.Count.ToString()); 就是这样的
      

  6.   

    --(1)你点击Button1的时候
    获取OrderID=5,ProductID=2这个2个参数
    传到数据库
    然后写存储过程create proc Return_Btn
    (
      @OrderID int,
      @ProductID int
    )
    as
    select * from dsOrders where OrderID=@OrderID and ProductID=@ProductID 
    return
    d
    --(2)跟第一问一样传"China"
      

  7.   


    第一个:DataTable dt = ds.Table["Details"];DataRow [] dr = dt.Select("OrderID == '5' and ProductID == '2'");第二个:
    DataRow [] dr = dt.Select("Description == 'China'");
    int i = dr.Count;
      

  8.   

    上面搞错了 '=='  换 '='第一个:DataTable dt = ds.Table["Details"];DataRow [] dr = dt.Select("OrderID = '5' and ProductID = '2'");第二个:
    DataRow [] dr = dt.Select("Description = 'China'");
    int i = dr.Count;
      

  9.   

    如果要在代码中实现的话
    就用
    //(1)
    DataView dv= dsOrders.Tables["Details"].DefaultView; 
    dv.RowFilter="OrderID=5 and ProductID=2"; 
      return dv.Count.ToString();
    //(2)
    dv.RowFilter="Description ='China'"; 
      

  10.   

    什么是不操作数据库啊
    if(dsOrders.Tables["Details"].rows[0][1]==5 and dsOrders.Tables["Details"].rows[0][1] == 2)
    return ture;
    else 
        return false;同理增加一个变量int i 控制Description == "China" 出现的次数,每出现一次i+1;
    这样算是操作数据库了么
      

  11.   

    1: 当我点Button1按钮的时候,判断dsOrders.Tables["Details"]表中的
    OrderID == 5 and ProductID == 2 这条记录是否存在??? 
    if(dsOrders.Tables["Details"].Select("OrderID == 5 AND ProductID == 2").Length >0)
    {
     messageBox.Show("记录存在!" );
    }
    else
    {
     messageBox.Show("记录不存在!" );
    }
    2: 当我点Button2按钮的时候,找出dsOrders.Tables["Details"]表中Description == "China" 共有几条记录??? 
    if(dsOrders.Tables["Details"].Select("Description == 'China'").Length >0)
    {
       int num = Convert.ToInt32(dsOrders.Tables["Details"].Select("Description == 'China'").Length);
        messageBox.Show(num.ToString()+"个记录存在!" );}
      

  12.   

    第一个、dsOrders.Tables["Details"].Select("OrderID = 5 and ProductID = 2").Length != 0 ? true : false第二个、dsOrders.Tables["Details"].Select("Description = 'China'").Length
      

  13.   

    dsOrders.Tables["Details"].Select("OrderID = 5 and ProductID = 2")
    dsOrders.Tables["Details"].Select("Description = 'China'")
      

  14.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            DataSet ds = new DataSet();
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                
                DataTable dt = new DataTable("Details");            DataColumn column;
                DataRow row;
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "OrderID";
                dt.Columns.Add(column);            column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "ProductID";
                dt.Columns.Add(column);            column = new DataColumn();
                column.DataType = System.Type.GetType("System.Decimal");
                column.ColumnName = "UnitPrice";
                dt.Columns.Add(column);            column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "Quantity";
                dt.Columns.Add(column);
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "Description";
                dt.Columns.Add(column);
               
                for (int i = 0; i < 100; i++)
                {
                    dt.Rows.Add(dt.NewRow());
                    dt.Rows[i][0] = i.ToString();
                    dt.Rows[i][1] = "2";
                    dt.Rows[i][2] = System.Decimal.Parse("11.11");
                    dt.Rows[i][3] = 1;
                    dt.Rows[i][4] = "China";
                }            ds.Merge(dt);        }        private void button1_Click(object sender, EventArgs e)
            {
               
                
                if (ds.Tables[0].Rows.Count != 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i][0].ToString() == "5" && ds.Tables[0].Rows[i][1].ToString() == "2")
                        {
                            MessageBox.Show("记录存在!");
                        }
                    }
                }
                
            }        private void button2_Click(object sender, EventArgs e)
            {
                int temp = 0;            if (ds.Tables[0].Rows.Count != 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i][4].ToString() == "China")
                        {
                            temp += 1;
                        }
                    }
                    if (temp == 0)
                    {
                        MessageBox.Show("无记录!");
                    }
                    else
                    {
                        MessageBox.Show("共有"+temp.ToString()+"条记录!");
                    }
                }
            }
        }
    }
    写了一段,你看看吧!