ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 미니프로젝트 정리 (내파트) Specific_Form폼, 통계 폼
    첫번째 프로젝트(요금제 추천) 2020. 7. 17. 19:56

    Specific_Form

    앞선 All_Products 에서 데이터 그리드뷰의 셀을 클릭하고 자세히를 누르게 되면 뜨는 폼이다. 요금제에 대한 상세한 혜택정보와 구매하기 버튼을 통해서 사용자의 성향을 변화시킨다.

     

     

    앞서 All_product 폼에서 자세히 버튼을 누를 때 Commons.SELECTED_MODEL을 키 값으로 

    위 두가지 테이블을 이너조인한 결과를 label에 띄워준다.

    public partial class Specific_Form : MetroFramework.Forms.MetroForm
        {
            public Specific_Form()
            {
                InitializeComponent();
            }
    
            private void Specific_Form_Load(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    DataSet ds = new DataSet();
                    conn.Open();
    
                    // 2개의 파라미터 정의 (항상 @로 시작)
                    string sql = " SELECT p.CallingPlan , p.Company , REPLACE(CONVERT(VARCHAR, CAST(p.Price AS MONEY), 1), '.00', '') AS Price, " +
                                      " p.Datas, d.Benefit " +
                                      " FROM productTbl AS p " +
                                      " INNER JOIN detailProductTbl AS d " +
                                      "  ON p.CallingPlan = d.CallingPlan " +
                                      "  WHERE p.CallingPlan =  @CallingPlan";
                    SqlCommand cmd = new SqlCommand(sql, conn);
    
                    // 각 파라미터 타입 및 값 입력
                    SqlParameter paramCity = new SqlParameter("@CallingPlan", SqlDbType.NVarChar, 50);
                    paramCity.Value = Commons.SELECTED_PLAN;
                    // SqlCommand 객체의 Parameters 속성에 추가
                    cmd.Parameters.Add(paramCity);
    
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds, "User_Information");
    
                    conn.Close();
                    infoTbl.DataSource = ds; //그리드의 데이터 소스에다가 붓는다.
                    infoTbl.DataMember = "User_Information";
                }
                
                lblModel.Text = infoTbl[0, 0].Value.ToString();
                lblModelNumber.Text = infoTbl[1, 0].Value.ToString();
                lblPrice.Text = infoTbl[2, 0].Value.ToString();
                lblProcesser.Text = infoTbl[3, 0].Value.ToString();
                lblMemory.Text = infoTbl[4, 0].Value.ToString();
    
                infoTbl.Visible = true;
    
            }

     

    취소 버튼을 누르면 창이 닫히는 이벤트와 구매 버튼을 눌렀을때 해당 요금제의 세가지 성향을 가져와서 Commons.User_STD1~3에 저장시켜놓은 세가지 성향과 비교하여 사용자의 성향을 조정한 후 데이터베이스에 업데이트한다.

     private void btnCancel_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void btnPurchase_Click(object sender, EventArgs e)
            {
                int p_std1 = 0,p_std2=0,p_std3=0;
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    DataSet ds = new DataSet();
                    conn.Open();
    
                    // 2개의 파라미터 정의 (항상 @로 시작)
                    string sql = " SELECT STD1,STD2,STD3 " +
                                      " FROM productTbl AS p " +
                                      "  WHERE CallingPlan =  @CallingPlan";
    
                    SqlCommand cmd = new SqlCommand(sql, conn);
    
             
                    SqlParameter paramCity = new SqlParameter("@CallingPlan", SqlDbType.NVarChar, 50);
                    paramCity.Value = Commons.SELECTED_PLAN;
                    cmd.Parameters.Add(paramCity);
    
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds, "User_Information");
    
                    conn.Close();
                    infoTbl.DataSource = ds; //그리드의 데이터 소스에다가 붓는다.
                    infoTbl.DataMember = "User_Information";
    
    
                }
                p_std1 = int.Parse(infoTbl[0, 0].Value.ToString());
                p_std2 = int.Parse(infoTbl[1, 0].Value.ToString());
                p_std3 = int.Parse(infoTbl[2, 0].Value.ToString());
    
    
                int a=0, b=0, c=0;
                if(Commons.USER_STD_1>0 && Commons.USER_STD_1<100)
                {
                    if (Commons.USER_STD_1 != p_std1)
                    {
                        a = Commons.USER_STD_1 + (p_std1 - Commons.USER_STD_1) / 5;
                    }
                    
                }
    
                if (Commons.USER_STD_2 > 0 && Commons.USER_STD_2 < 100)
                {
                    if (Commons.USER_STD_2 != p_std2)
                    {
                       
                            b = Commons.USER_STD_2 + (p_std2 - Commons.USER_STD_2) / 5;
               
                        
                    }
                }
                if (Commons.USER_STD_3 > 0 && Commons.USER_STD_3 < 100)
                {
                    if (Commons.USER_STD_3 != p_std3)
                    {
    
                        c = Commons.USER_STD_3 + (p_std3 - Commons.USER_STD_3) / 5;
    
    
                    }
                }
    
    
                MetroMessageBox.Show(this,"구매가 완료되었습니다. ", "구매 완료", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                //추가
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "UPDATE productTbl SET SoldCallPlan = SoldCallPlan +1 WHERE CallingPlan = @CallingPlan ";
    
                    SqlParameter paramplan = new SqlParameter("@CallingPlan", SqlDbType.NVarChar, 50);
                    paramplan.Value = Commons.SELECTED_PLAN;
                    cmd.Parameters.Add(paramplan);
                    cmd.ExecuteNonQuery();
                }
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
    
    
                    cmd.CommandText = " UPDATE dbo.userTbl " +
                                      " SET userUSCallPlan = @userUSCallPlan, " +
                                      " STD1 = @STD1, STD2 = @STD2, STD3 = @STD3 " +
                                      " WHERE userID = @userID";
    
                    SqlParameter parmuserUSCallPlan = new SqlParameter("@userUSCallPlan", SqlDbType.NVarChar, 50);
                    parmuserUSCallPlan.Value = Commons.SELECTED_PLAN;
                    cmd.Parameters.Add(parmuserUSCallPlan);
    
                    SqlParameter parmSTD1 = new SqlParameter("@STD1", SqlDbType.Int);
                    parmSTD1.Value = a;
                    cmd.Parameters.Add(parmSTD1);
    
                    SqlParameter parSTD2 = new SqlParameter("@STD2", SqlDbType.Int);
                    parSTD2.Value = b;
                    cmd.Parameters.Add(parSTD2);
    
                    SqlParameter parmSTD3 = new SqlParameter("@STD3", SqlDbType.Int);
                    parmSTD3.Value = c;
                    cmd.Parameters.Add(parmSTD3);
    
                    SqlParameter parmuserID = new SqlParameter("@userID", SqlDbType.VarChar,50);
                    parmuserID.Value = Commons.LOGINUSERID;
                    cmd.Parameters.Add(parmuserID);
    
                    cmd.ExecuteNonQuery();//excute는 넣을 때 쓰는건 NonQuery 그 외에건 가져올 때
    
                    Close();
                   
                }
    
            }
        }
    }

    다음은 전체 소스코드이다.

    using MetroFramework;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    namespace MiniProject
    {
        public partial class Specific_Form : MetroFramework.Forms.MetroForm
        {
            public Specific_Form()
            {
                InitializeComponent();
            }
    
            private void Specific_Form_Load(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    DataSet ds = new DataSet();
                    conn.Open();
    
                    // 2개의 파라미터 정의 (항상 @로 시작)
                    string sql = " SELECT p.CallingPlan , p.Company , REPLACE(CONVERT(VARCHAR, CAST(p.Price AS MONEY), 1), '.00', '') AS Price, " +
                                      " p.Datas, d.Benefit " +
                                      " FROM productTbl AS p " +
                                      " INNER JOIN detailProductTbl AS d " +
                                      "  ON p.CallingPlan = d.CallingPlan " +
                                      "  WHERE p.CallingPlan =  @CallingPlan";
                    SqlCommand cmd = new SqlCommand(sql, conn);
    
                    // 각 파라미터 타입 및 값 입력
                    SqlParameter paramCity = new SqlParameter("@CallingPlan", SqlDbType.NVarChar, 50);
                    paramCity.Value = Commons.SELECTED_PLAN;
                    // SqlCommand 객체의 Parameters 속성에 추가
                    cmd.Parameters.Add(paramCity);
    
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds, "User_Information");
    
                    conn.Close();
                    infoTbl.DataSource = ds; //그리드의 데이터 소스에다가 붓는다.
                    infoTbl.DataMember = "User_Information";
                }
                
                lblModel.Text = infoTbl[0, 0].Value.ToString();
                lblModelNumber.Text = infoTbl[1, 0].Value.ToString();
                lblPrice.Text = infoTbl[2, 0].Value.ToString();
                lblProcesser.Text = infoTbl[3, 0].Value.ToString();
                lblMemory.Text = infoTbl[4, 0].Value.ToString();
    
                infoTbl.Visible = true;
    
            }
    
            private void btnCancel_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void btnPurchase_Click(object sender, EventArgs e)
            {
                int p_std1 = 0,p_std2=0,p_std3=0;
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    DataSet ds = new DataSet();
                    conn.Open();
    
                    // 2개의 파라미터 정의 (항상 @로 시작)
                    string sql = " SELECT STD1,STD2,STD3 " +
                                      " FROM productTbl AS p " +
                                      "  WHERE CallingPlan =  @CallingPlan";
    
                    SqlCommand cmd = new SqlCommand(sql, conn);
    
             
                    SqlParameter paramCity = new SqlParameter("@CallingPlan", SqlDbType.NVarChar, 50);
                    paramCity.Value = Commons.SELECTED_PLAN;
                    cmd.Parameters.Add(paramCity);
    
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds, "User_Information");
    
                    conn.Close();
                    infoTbl.DataSource = ds; //그리드의 데이터 소스에다가 붓는다.
                    infoTbl.DataMember = "User_Information";
    
    
                }
                p_std1 = int.Parse(infoTbl[0, 0].Value.ToString());
                p_std2 = int.Parse(infoTbl[1, 0].Value.ToString());
                p_std3 = int.Parse(infoTbl[2, 0].Value.ToString());
    
    
                int a=0, b=0, c=0;
                if(Commons.USER_STD_1>0 && Commons.USER_STD_1<100)
                {
                    if (Commons.USER_STD_1 != p_std1)
                    {
                        a = Commons.USER_STD_1 + (p_std1 - Commons.USER_STD_1) / 5;
                    }
                    
                }
    
                if (Commons.USER_STD_2 > 0 && Commons.USER_STD_2 < 100)
                {
                    if (Commons.USER_STD_2 != p_std2)
                    {
                       
                            b = Commons.USER_STD_2 + (p_std2 - Commons.USER_STD_2) / 5;
               
                        
                    }
                }
                if (Commons.USER_STD_3 > 0 && Commons.USER_STD_3 < 100)
                {
                    if (Commons.USER_STD_3 != p_std3)
                    {
    
                        c = Commons.USER_STD_3 + (p_std3 - Commons.USER_STD_3) / 5;
    
    
                    }
                }
    
    
                MetroMessageBox.Show(this,"구매가 완료되었습니다. ", "구매 완료", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                //추가
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "UPDATE productTbl SET SoldCallPlan = SoldCallPlan +1 WHERE CallingPlan = @CallingPlan ";
    
                    SqlParameter paramplan = new SqlParameter("@CallingPlan", SqlDbType.NVarChar, 50);
                    paramplan.Value = Commons.SELECTED_PLAN;
                    cmd.Parameters.Add(paramplan);
                    cmd.ExecuteNonQuery();
                }
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
    
    
                    cmd.CommandText = " UPDATE dbo.userTbl " +
                                      " SET userUSCallPlan = @userUSCallPlan, " +
                                      " STD1 = @STD1, STD2 = @STD2, STD3 = @STD3 " +
                                      " WHERE userID = @userID";
    
                    SqlParameter parmuserUSCallPlan = new SqlParameter("@userUSCallPlan", SqlDbType.NVarChar, 50);
                    parmuserUSCallPlan.Value = Commons.SELECTED_PLAN;
                    cmd.Parameters.Add(parmuserUSCallPlan);
    
                    SqlParameter parmSTD1 = new SqlParameter("@STD1", SqlDbType.Int);
                    parmSTD1.Value = a;
                    cmd.Parameters.Add(parmSTD1);
    
                    SqlParameter parSTD2 = new SqlParameter("@STD2", SqlDbType.Int);
                    parSTD2.Value = b;
                    cmd.Parameters.Add(parSTD2);
    
                    SqlParameter parmSTD3 = new SqlParameter("@STD3", SqlDbType.Int);
                    parmSTD3.Value = c;
                    cmd.Parameters.Add(parmSTD3);
    
                    SqlParameter parmuserID = new SqlParameter("@userID", SqlDbType.VarChar,50);
                    parmuserID.Value = Commons.LOGINUSERID;
                    cmd.Parameters.Add(parmuserID);
    
                    cmd.ExecuteNonQuery();//excute는 넣을 때 쓰는건 NonQuery 그 외에건 가져올 때
    
                    Close();
                   
                }
    
            }
        }
    }
    

     

    AdStatisticsForm_Form

     

     

    뭔가 허전해서 더하기로 한 통계창이다.

     

    왼쪽 위의 막대그래프부터 설명하자면 데이터베이스에 나와있는 요금제 별 구매수

    를 쿼리문(두번째 using) 으로 셀렉트할 때 CallingPlan을 키 값으로 SoldCallPlan을 내림차순으로 정렬해서 상위 5개의 요금제를 추출해서 그래프로 띄웠다. 첫번째 using문은 usertbl에서 해당 프로그램을 사용중인 사용자의 숫자를 알아내서 usercount변수에 저장하기 위한 부분이다. 오른쪽 위 그래프의 데이터를 추출하는데 사용된다.

    private void AdStatisticsForm_Load(object sender, EventArgs e)
            {
    
                Color[] color_decision = new Color[10];
                color_decision[0] = Color.Red;
                color_decision[1] = Color.Blue;
                color_decision[2] = Color.LightGray;
                color_decision[3] = Color.Beige;
                color_decision[4] = Color.BlueViolet;
                color_decision[5] = Color.FloralWhite;
                color_decision[6] = Color.Firebrick;
                color_decision[7] = Color.Brown;
                color_decision[8] = Color.Coral;
                color_decision[9] = Color.Chocolate;
    
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    string strQuery = " SELECT num " +
                                      "  FROM userTbl ORDER BY num DESC ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    reader.Read();
    
                    usercount = int.Parse(reader[0].ToString());
                }
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    string strQuery = " SELECT CallingPlan, SoldCallPlan " +
                                      "  FROM productTbl " +
                                      "  ORDER BY SoldCallPlan DESC ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    for (int i = 0; i < 5; i++)
                    {
                        reader.Read();
    
                        CallingName[i] = reader[0].ToString();
                        count[i] = int.Parse(reader[1].ToString());
                    }
    
                    for (int i = 0; i < 5; i++)
                    {
                        chart1.Series["Series1"].Points.Add(count[i]);
                        chart1.Series["Series1"].Points[i].Color = color_decision[i];
                        chart1.Series["Series1"].Points[i].AxisLabel = CallingName[i];
                    }
                }

     

     

    아래는 왼쪽 밑 그래프를 그리는데 사용된다. 사용자의 성향 정보를 SELECT문으로 추출해서 세개중에서 가장 큰 값을 하나씩 더한다. 만약 가장 큰 값이 두개, 혹은 세개라면, 두개 세개 다 더한다. 다 더했다면 백분율 내서 그래프의 항목에 하나씩 Add한다.

    using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    string strQuery = " SELECT STD1,STD2,STD3 " +
                                      "  FROM userTbl ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    int std1 = 0, std2 = 0, std3 = 0;
                    int standard1 = 0, standard2 = 0, standard3 = 0;
    
                    for (int i = 0; i < usercount; i++)
                    {
                        reader.Read();
    
                        std1 = int.Parse(reader[0].ToString());
                        std2 = int.Parse(reader[1].ToString());
                        std3 = int.Parse(reader[2].ToString());
    
                        if (std1 != std2 && std1 != std3 && std2 != std3)
                        {
                            if (std1 > std2 && std1 > std3)
                                standard1 += 1;
                            if (std2 > std1 && std2 > std3)
                                standard2 += 1;
                            if (std3 > std1 && std3 > std2)
                                standard3 += 1;
                        }
                        else if (std1 == std2 || std1 == std3 || std2 == std3)
                        {
                            if (std1 == std2)
                            {
                                if (std1 > std3)
                                {
                                    standard1 += 1;
                                    standard2 += 1;
                                }
                                else
                                    standard3 += 1;
                            }
                            if (std1 == std3)
                            {
                                if (std1 > std2)
                                {
                                    standard1 += 1;
                                    standard3 += 1;
                                }
                                else
                                    standard2 += 1;
                            }
                            if (std2 == std3)
                            {
                                if (std2 > std1)
                                {
                                    standard2 += 1;
                                    standard3 += 1;
                                }
                                else
                                    standard1 += 1;
                            }
                        }
                    }
    
                    double sum = standard1 + standard2 + standard3;
                    double dstd1 = (standard1 / sum) * 100;
                    double dstd2 = (standard2 / sum) * 100;
                    double dstd3 = (standard3 / sum) * 100;
    
                    dstd1 = Math.Round(dstd1, 2);
                    dstd2 = Math.Round(dstd2, 2);
                    dstd3 = Math.Round(dstd3, 2);
    
                    chart2.Series["Series1"].Points.Add(dstd1);
                    chart2.Series["Series1"].Points[0].Color = color_decision[0];
                    chart2.Series["Series1"].Points[0].AxisLabel = dstd1.ToString()+" %";
                    chart2.Series["Series1"].Points[0].LegendText = "가격";
    
                    chart2.Series["Series1"].Points.Add(dstd2);
                    chart2.Series["Series1"].Points[1].Color = color_decision[1];
                    chart2.Series["Series1"].Points[1].AxisLabel = dstd2.ToString()+" %";
                    chart2.Series["Series1"].Points[1].LegendText = "데이터";
    
                    chart2.Series["Series1"].Points.Add(dstd3);
                    chart2.Series["Series1"].Points[2].Color = color_decision[2];
                    chart2.Series["Series1"].Points[2].AxisLabel = dstd3.ToString() + " %";
                    chart2.Series["Series1"].Points[2].LegendText = "혜택";
                }

    마지막으로 통신사 점유율이다. userTbl에는 통신사가 나와있지 않기 때문에 요금제를 키 값으로 userTbl과 productTbl을 이너조인해서 통신사의 갯수를 백분율 내서 그래프에 항목에 하나씩 Add했다.

     

     using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    string[] Company = new string[usercount];
                    double skt = 0;
                    double kt = 0;
                    double lg = 0;
    
                    conn.Open();
                    string strQuery = "  SELECT p.Company " +
                                      "  FROM userTbl AS u " +
                                      "  INNER JOIN productTbl AS p " +
                                      "  ON u.userUSCallPlan = p.CallingPlan  ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    for (int i = 0; i < usercount; i++)
                    {
                        reader.Read();
    
                        Company[i] = reader[0].ToString();
    
                        if (Company[i] == "SKT")
                            skt = skt + 1;
                        if (Company[i] == "KT")
                            kt = kt + 1;
                        if (Company[i] == "LGT")
                            lg = lg + 1;
                    }
    
                    double sum = 0;
    
                    sum = skt + kt + lg;
                    skt = skt / sum * 100;
    
                    kt = kt / sum * 100;
                    lg = lg / sum * 100;
    
                    double skt1 = Math.Round(skt, 2);
                    double kt1 = Math.Round(kt, 2);
                    double lg1 = Math.Round(lg, 2);
    
                    chart3.Series["Series1"].Points.Add(skt1);
                    chart3.Series["Series1"].Points[0].Color = color_decision[0];
                    chart3.Series["Series1"].Points[0].AxisLabel = skt1.ToString() + "%";
                    chart3.Series["Series1"].Points[0].LegendText = "SKT";
    
                    chart3.Series["Series1"].Points.Add(kt1);
                    chart3.Series["Series1"].Points[1].Color = color_decision[1];
                    chart3.Series["Series1"].Points[1].AxisLabel = kt1.ToString() + "%";
                    chart3.Series["Series1"].Points[1].LegendText = "KT";
    
                    chart3.Series["Series1"].Points.Add(lg1);
                    chart3.Series["Series1"].Points[2].Color = color_decision[2];
                    chart3.Series["Series1"].Points[2].AxisLabel = lg1.ToString() + "%";
                    chart3.Series["Series1"].Points[2].LegendText = "LGT";
                }

     

    그래프의 Series에서 속성을 다음과 같이 건드리면 원형그래프가 된다.

     

    다음은 전체 소스코드이다.

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace MiniProject
    {
        public partial class AdStatisticsForm : Form
        {
            string[] CallingName = new string[10];
            int[] count = new int[10];
            int usercount = 0;
    
            public AdStatisticsForm()
            {
                InitializeComponent();
            }
    
            private void AdStatisticsForm_Load(object sender, EventArgs e)
            {
    
                Color[] color_decision = new Color[10];
                color_decision[0] = Color.Red;
                color_decision[1] = Color.Blue;
                color_decision[2] = Color.LightGray;
                color_decision[3] = Color.Beige;
                color_decision[4] = Color.BlueViolet;
                color_decision[5] = Color.FloralWhite;
                color_decision[6] = Color.Firebrick;
                color_decision[7] = Color.Brown;
                color_decision[8] = Color.Coral;
                color_decision[9] = Color.Chocolate;
    
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    string strQuery = " SELECT num " +
                                      "  FROM userTbl ORDER BY num DESC ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    reader.Read();
    
                    usercount = int.Parse(reader[0].ToString());
                }
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    string strQuery = " SELECT CallingPlan, SoldCallPlan " +
                                      "  FROM productTbl " +
                                      "  ORDER BY SoldCallPlan DESC ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    for (int i = 0; i < 5; i++)
                    {
                        reader.Read();
    
                        CallingName[i] = reader[0].ToString();
                        count[i] = int.Parse(reader[1].ToString());
                    }
    
                    for (int i = 0; i < 5; i++)
                    {
                        chart1.Series["Series1"].Points.Add(count[i]);
                        chart1.Series["Series1"].Points[i].Color = color_decision[i];
                        chart1.Series["Series1"].Points[i].AxisLabel = CallingName[i];
                    }
                }
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    conn.Open();
                    string strQuery = " SELECT STD1,STD2,STD3 " +
                                      "  FROM userTbl ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    int std1 = 0, std2 = 0, std3 = 0;
                    int standard1 = 0, standard2 = 0, standard3 = 0;
    
                    for (int i = 0; i < usercount; i++)
                    {
                        reader.Read();
    
                        std1 = int.Parse(reader[0].ToString());
                        std2 = int.Parse(reader[1].ToString());
                        std3 = int.Parse(reader[2].ToString());
    
                        if (std1 != std2 && std1 != std3 && std2 != std3)
                        {
                            if (std1 > std2 && std1 > std3)
                                standard1 += 1;
                            if (std2 > std1 && std2 > std3)
                                standard2 += 1;
                            if (std3 > std1 && std3 > std2)
                                standard3 += 1;
                        }
                        else if (std1 == std2 || std1 == std3 || std2 == std3)
                        {
                            if (std1 == std2)
                            {
                                if (std1 > std3)
                                {
                                    standard1 += 1;
                                    standard2 += 1;
                                }
                                else
                                    standard3 += 1;
                            }
                            if (std1 == std3)
                            {
                                if (std1 > std2)
                                {
                                    standard1 += 1;
                                    standard3 += 1;
                                }
                                else
                                    standard2 += 1;
                            }
                            if (std2 == std3)
                            {
                                if (std2 > std1)
                                {
                                    standard2 += 1;
                                    standard3 += 1;
                                }
                                else
                                    standard1 += 1;
                            }
                        }
                    }
    
                    double sum = standard1 + standard2 + standard3;
                    double dstd1 = (standard1 / sum) * 100;
                    double dstd2 = (standard2 / sum) * 100;
                    double dstd3 = (standard3 / sum) * 100;
    
                    dstd1 = Math.Round(dstd1, 2);
                    dstd2 = Math.Round(dstd2, 2);
                    dstd3 = Math.Round(dstd3, 2);
    
                    chart2.Series["Series1"].Points.Add(dstd1);
                    chart2.Series["Series1"].Points[0].Color = color_decision[0];
                    chart2.Series["Series1"].Points[0].AxisLabel = dstd1.ToString()+" %";
                    chart2.Series["Series1"].Points[0].LegendText = "가격";
    
                    chart2.Series["Series1"].Points.Add(dstd2);
                    chart2.Series["Series1"].Points[1].Color = color_decision[1];
                    chart2.Series["Series1"].Points[1].AxisLabel = dstd2.ToString()+" %";
                    chart2.Series["Series1"].Points[1].LegendText = "데이터";
    
                    chart2.Series["Series1"].Points.Add(dstd3);
                    chart2.Series["Series1"].Points[2].Color = color_decision[2];
                    chart2.Series["Series1"].Points[2].AxisLabel = dstd3.ToString() + " %";
                    chart2.Series["Series1"].Points[2].LegendText = "혜택";
                }
    
                using (SqlConnection conn = new SqlConnection(Commons.CONNSTRING))
                {
                    string[] Company = new string[usercount];
                    double skt = 0;
                    double kt = 0;
                    double lg = 0;
    
                    conn.Open();
                    string strQuery = "  SELECT p.Company " +
                                      "  FROM userTbl AS u " +
                                      "  INNER JOIN productTbl AS p " +
                                      "  ON u.userUSCallPlan = p.CallingPlan  ";
    
                    SqlCommand cmd = new SqlCommand(strQuery, conn);
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
    
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds, "sold");
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    for (int i = 0; i < usercount; i++)
                    {
                        reader.Read();
    
                        Company[i] = reader[0].ToString();
    
                        if (Company[i] == "SKT")
                            skt = skt + 1;
                        if (Company[i] == "KT")
                            kt = kt + 1;
                        if (Company[i] == "LGT")
                            lg = lg + 1;
                    }
    
                    double sum = 0;
    
                    sum = skt + kt + lg;
                    skt = skt / sum * 100;
    
                    kt = kt / sum * 100;
                    lg = lg / sum * 100;
    
                    double skt1 = Math.Round(skt, 2);
                    double kt1 = Math.Round(kt, 2);
                    double lg1 = Math.Round(lg, 2);
    
                    chart3.Series["Series1"].Points.Add(skt1);
                    chart3.Series["Series1"].Points[0].Color = color_decision[0];
                    chart3.Series["Series1"].Points[0].AxisLabel = skt1.ToString() + "%";
                    chart3.Series["Series1"].Points[0].LegendText = "SKT";
    
                    chart3.Series["Series1"].Points.Add(kt1);
                    chart3.Series["Series1"].Points[1].Color = color_decision[1];
                    chart3.Series["Series1"].Points[1].AxisLabel = kt1.ToString() + "%";
                    chart3.Series["Series1"].Points[1].LegendText = "KT";
    
                    chart3.Series["Series1"].Points.Add(lg1);
                    chart3.Series["Series1"].Points[2].Color = color_decision[2];
                    chart3.Series["Series1"].Points[2].AxisLabel = lg1.ToString() + "%";
                    chart3.Series["Series1"].Points[2].LegendText = "LGT";
                }
            }
        }
    }
    

     

     

     

     

Designed by Tistory.