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();

        }

    }

}

结果如下所示:

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

你可能感兴趣的:(Data)