1. 程式人生 > 其它 >sql豎錶轉橫表

sql豎錶轉橫表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using TISCOMQTT.model;
using TISCOMQTT.components;
using System.Windows.Forms;

namespace TISCOMQTT.components
{
    internal class conSQL
    {
        public static List<List<table1>> llt=new List<List<table1>>();
        
        public static DataSet readTables()
        {
            DataSet ds=new DataSet();
            llt.Add(Form1.M1);
            llt.Add(Form1.M2);
            //llt.Add(Form1.M3);
            //llt.Add(Form1.M4);
            //llt.Add(Form1.M5);
            //llt.Add(Form1.M6);
            //llt.Add(Form1.M7);
            //llt.Add(Form1.M8);
            //llt.Add(Form1.M9);
            //llt.Add(Form1.M10);
            //llt.Add(Form1.M11);
            //llt.Add(Form1.M12);
            //llt.Add(Form1.M13);

            try
            {
                foreach(List<table1> table in llt)
                {
                    String sql = "";

                    String part1SQL = "";

                    String part2SQL = "";

                    DataTable dt=new DataTable();

                    foreach(table1 t in table)
                    {
                        if (table.IndexOf(t) < 5)
                        {
                            sql = sql + "main." + t.name + ",";

                            part1SQL = part1SQL + t.name + ",";
                        }
                        else
                        {
                            //sql += "sub." + t.name + ",";

                            part2SQL = part2SQL + "sum(case PROD_ITEM_CODE when " + "'" + t.name + "'" + " then cast(PROD_ITEM_VALUE as decimal(18,2)) else '0' end) as " + t.name + ",";
                        }
                    }

                    String executeThreeSQL = "select main.MAIN_ID," + 
                        sql + "sub.PROD_ITEM_CODE,sub.PROD_ITEM_VALUE "+
                        " from TB_QUALITY_MAIN main,TB_QUALITY_SUB sub where main.MAIN_ID=sub.MAIN_ID";

                    String executeOneSQL = "select " + part1SQL;

                    String executeTwoSQL = part2SQL;

                    String executeSQL = executeOneSQL + 
                        executeTwoSQL.Substring(0, executeTwoSQL.Count() - 1) + 
                        " from ( " + executeThreeSQL + " ) test group by " 
                        + part1SQL.Substring(0, part1SQL.Count() - 1) + ";";
                    LogHelper.writeLog(executeSQL);


                    String con = "Data Source=DESKTOP-L321ML9;Initial Catalog=LZ4RX;User ID=sa;Password=19910909";
                    using (SqlConnection conn = new SqlConnection(con))
                    {
                        conn.Open();
                        SqlDataAdapter adp = new SqlDataAdapter(executeSQL, conn);
                        adp.Fill(dt);
                        conn.Close();
                        ds.Tables.Add(dt);
                    }
                }
                return ds;



                //String xmlPath = @"D:\Code\PLC\Agreement\TISCOMQTT\data\table1.xml";
                //IEnumerable<table1> it = ReadXML.read_xml(xmlPath);
                //String _partSQL = "";
                //foreach(var t in it)
                //{
                //    _partSQL = _partSQL + t.name + ",";
                //}
                //int len = _partSQL.Length;
                //String partSQL = _partSQL.Substring(0, len - 1);


                //String con = "Data Source=DESKTOP-L321ML9;Initial Catalog=LGLZ;User ID=sa;Password=19910909";
                
            }
            catch(Exception ex)
            {
                MessageBox.Show("查詢資料庫錯誤:"+ex.Message,"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return null;
            }
            








        }
    }
}