1. 程式人生 > >【機房重構】——組合查詢

【機房重構】——組合查詢

建立父窗體

構造虛方法

//將漢字轉化為資料庫識別的形式
public virtual string ToEnglish(string ctr)
{
    return "";
}

//獲得資料庫名字
public virtual string GetdbName()
{
    return "";
}

//顯示資料
protected virtual void ToDgv(Entity.GroupQuery group)
{

}

點選查詢

        private void btnQuery_Click(object sender, EventArgs e)
        {     
            #region 給實體賦值
            Facade.GroupQueryFacade facade = new Facade.GroupQueryFacade();
            Entity.GroupQuery group = new Entity.GroupQuery();

            //從資料庫中獲取表格(選擇呼叫哪一個資料庫)
            group.GetDBName = GetdbName();

            group.comboBox1 = ToEnglish(comboBox1.Text.Trim());
            group.comboBox2 = ToEnglish(comboBox2.Text.Trim());
            group.comboBox3 = ToEnglish(comboBox3.Text.Trim());

            group.comboBox7 = ToEnglish(comboBox7.Text.Trim());
            group.comboBox8 = ToEnglish(comboBox8.Text.Trim());

            group.comboBox4 = comboBox4.Text.Trim();
            group.comboBox5 = comboBox5.Text.Trim();
            group.comboBox6 = comboBox6.Text.Trim();

            group.textBox1 = textBox1.Text.Trim();
            group.textBox2 = textBox2.Text.Trim();
            group.textBox3 = textBox3.Text.Trim();

            //虛方法ToDgv,只實現了實現這個方法的傳遞
            //他實現給在子類中傳值,在子類中返回表顯示出來
            

            DataTable result = facade.GroupQuery(group);
            if (result.Rows.Count != 0)
            {
                ToDgv(group);//查詢到以後呼叫子窗體中重寫的方法顯示資料
                MessageBox.Show("查詢成功!", "提示");                
            }
            else
            {
                MessageBox.Show("您查詢的資訊不存在", "提示");
            }
            #endregion
            
        }

建立子窗體

重寫

//重寫,表示查詢的是studentInfo
        public override string GetdbName()
        {
            return "studentInfo";
        }
        //重寫虛方法
        public override string ToEnglish(string combo)
        {
            switch (combo)
            {
                case "學號":
                    return "UserID";
                case "姓名":
                    return "UserName";
                case "性別":
                    return "Sex";
                case "系別":
                    return "Department";
                case "年級":
                    return "Grade";
                case "班級":
                    return "Class";
                case "與":
                    return "and";
                case "或":
                    return "or";
                default:
                    return "";
            }
        }

        //重寫,顯示查詢結果
        protected override void ToDgv(Entity.GroupQuery group)
        {
            dataGridView1.DataSource = "";
            Facade.GroupQueryFacade facade = new Facade.GroupQueryFacade();

            DataTable table = facade.GroupQuery(group);
            if (table.Rows.Count == 0)
            {
                MessageBox.Show("沒有記錄");
            }
            else
            {
                dataGridView1.DataSource = table;
                dataGridView1.Refresh();
            }
        }