• DataSet之将连个DataSet集合合并成一个DataSet集合


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace qqIsonline
    {
        public partial class index1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack){
                    GridView1.DataSource = GetQQinfo();
                    GridView1.DataBind();
                    GridView2.DataSource = GetorderInfo();
                    GridView2.DataBind();
                    GridView3.DataSource = GetDataByCount();
                    GridView3.DataBind();
                }
                
            }
            string sqlConnStr = ConfigurationManager.ConnectionStrings["myDB"].ToString();
    
            //获得QQ信息
            public DataSet GetQQinfo()
            {
               using(SqlConnection sqlConn = new SqlConnection(sqlConnStr)){
                   string sqlStr = "select * from qqinfo";
                   SqlDataAdapter sda = new SqlDataAdapter(sqlStr,sqlConn);
                   DataSet ds = new DataSet();
                   sda.Fill(ds);
                   return ds;
               }
            }
    
            //获得订单中每个扣扣号出现的总数
            public DataSet GetorderInfo()
            {
                using (SqlConnection sqlConn = new SqlConnection(sqlConnStr))
                {
                    string sqlStr = "select COUNT(orderinfo.Id) as num,QQ from orderinfo group by QQ order by num Desc";
                    SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConn);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    return ds;
                }
            }
    
            /// <summary>
            /// 将两个DataSet中的数据整合
            /// 将每个客服对应的订单量追加到DataTable后面
            /// </summary>
            /// <returns>一个新的集合</returns>
            public DataTable GetDataByCount()
            {
                DataSet dtordet = GetorderInfo();     //每个客服订单量汇总的集合
                DataSet dtqq = GetQQinfo();           //客服数据集合
                dtqq.Tables[0].Columns.Add("colunm", typeof(string));    //给客服集合新增一列
                for (int i = 0; i < dtordet.Tables[0].Rows.Count; i++)   //遍历每个客服订单量汇总的集合
                {
                    string qq = dtordet.Tables[0].Rows[i]["QQ"].ToString();   //获得该行的QQ号
                    DataRow[] dr = dtqq.Tables[0].Select("QQ=" + qq);         //客服数据集合中是否包含有这个ID
                    if (dr.Length.Equals(1))   //包含
                    {
                        int k = dtqq.Tables[0].Rows.IndexOf(dr[0]);        //记录下该行数据的行号!
                        string num = dtordet.Tables[0].Rows[k]["num"].ToString();   //获得改行数据对应的订单数
                        dtqq.Tables[0].Rows[i]["colunm"] = num;   //添加列数据
                    }
                }
                DataView dv = dtqq.Tables[0].DefaultView;
                dv.Sort="colunm DESC";   //对数据根据订单量进行降序排序
                return dv.ToTable();
            }
        }
    }

    结果如下所示:

  • 相关阅读:
    android 线程的优先级设置方法
    android插件开发
    针对AsyncTask的分析
    针对平板的扩展
    adb shell dumpsys activity
    gzip相关【转帖】
    android中也需要SQL语句
    monkey脚本
    eclipse 引入外部包不能运行 NoClassDefFoundError 的问题
    翻译 : fragment
  • 原文地址:https://www.cnblogs.com/Yisijun/p/4656343.html
Copyright © 2020-2023  润新知