Example #1
0
        private void FillDataGrid()
        {
            string ConString = MainWindow.GetString();
            string CmdString = string.Empty;

            using (SqlConnection con = new SqlConnection(ConString))
            {
                try
                {
                    if (m_addr_a.IsChecked == true)
                    {   //  주소 검색
                        string tmpstr = searchText.Text;
                        CmdString = "SELECT MNumber = (mn_sign + (case when mn_exp < 0 then 'E' else 'E+' end) + cast(mn_exp as char(12))), mn_exp, m_addr_a, mother_create_date FROM mothertbl nolock where m_addr_a like '%" + tmpstr + "%'";
                    }
                    else
                    {   //  숫자 검색
                        //  파싱을 통해 문법 준수 여부 체크
                        string  sign = searchText.Text.Trim();
                        decimal dd   = decimal.Parse(sign, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);

                        BigDecimal bf;
                        int        exp  = 0;
                        int        eidx = sign.IndexOf('E');
                        if (eidx != -1)
                        {   //  가수 예외 처리
                            bf = BigDecimal.Parse(sign.Substring(0, eidx));
                            if (bf == 0)
                            {
                                throw new Exception("0은 검색할 수 없습니다.");
                            }
                            else if (bf < 1)
                            {
                                throw new Exception("지수가 있는 수의 경우 1 미만은 허용되지 않습니다.");
                            }
                            else if (bf >= 10)
                            {
                                throw new Exception("지수가 있는 수의 경우 10 이상은 허용되지 않습니다.");
                            }
                            else
                            {
                                exp = int.Parse(sign.Substring(eidx + 1));
                            }
                        }
                        else
                        {   //  지수와 가수 분리 작업
                            bf  = BigDecimal.Parse(sign.Substring(0, eidx));
                            exp = int.Parse(sign.Substring(eidx + 1));
                            while (bf > 10 || bf < 1)
                            {
                                if (bf > 10)
                                {
                                    bf /= 10;
                                    ++exp;
                                }
                                else if (bf < 1)
                                {
                                    bf *= 10;
                                    --exp;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        sign = bf.ToString();
                        string msgstr = sign + (exp > 0 ? "E+" : "E") + exp;
                        CmdString = "SELECT MNumber = (mn_sign + (case when mn_exp < 0 then 'E' else 'E+' end) + cast(mn_exp as char(12))) , mn_exp, m_addr_a, mother_create_date FROM mothertbl nolock where mn_sign = '"
                                    + sign + ((mn_sign.IsChecked == true) ? "'" : "' and mn_exp = " + exp);
                    }
                    SqlCommand     cmd = new SqlCommand(CmdString, con);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable      dt  = new DataTable("MotherNumbers");
                    sda.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("결과가 없습니다.", "알림");
                        datagrid1.ItemsSource = null;
                    }
                    else
                    {
                        dt.DefaultView.Sort   = "mother_create_date desc";
                        datagrid1.ItemsSource = dt.DefaultView;
                    }
                } catch (Exception e)
                {
                    MessageBox.Show(e.Message, "오류");
                }   //  end try-catch
            }
        }