iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
自我挑戰組

菜鳥大學生資料庫學習記系列 第 29

Day 29 -資料庫應用小程式 菜單顯示(內涵程式碼)

廢話不多說直接開始

我們點選菜單按鈕會連結到這個表單
https://ithelp.ithome.com.tw/upload/images/20211013/20141567U0q8Or3yUe.jpg

全域變數

   string MyConnection2 = "datasource=122.0.0.0;port=3366;username=root;password=;Allow Zero Datetime=True;";
        private string dbHost = "localhost";
        private string dbPort = "3306";
        private string dbUser = "root";
        private string dbPassword = ""; 
        private string dbName = "db85cc"; 
        MySqlConnection conn = null;
        MySqlDataAdapter mySqlDataAdapter;
        DataSet ds;

按鈕按照菜單內商品去做分類

麵包按鈕:

 try
                {

                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='bread'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2); 
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable;             
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

切片蛋糕:

    try
                {

                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='cake'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable;          
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

大杯飲料:

     try
                {

                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='drinkL'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2); 
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable; 
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

中杯飲料:

try
                { 
                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='drinkM'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable;  
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

小杯飲料:

 try
                {
                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='drinkS'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable;           
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

六吋蛋糕:

try
                {
                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='cake6'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable; 
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

八吋蛋糕:

try
                {
                string Query = "select product.Product_name,product.Product_price from db85cc.product where product.prodict_id='cake8'";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand2;
                DataTable dTable = new DataTable();
                MyAdapter.Fill(dTable);
                dgvmenu.DataSource = dTable; 
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }

實測

https://ithelp.ithome.com.tw/upload/images/20211013/20141567Qxhe69PZS0.jpg

https://ithelp.ithome.com.tw/upload/images/20211013/20141567TWn1QlwQgH.jpg

下篇見~~


上一篇
Day 28 -資料庫應用小程式 會員新增刪除(內涵程式碼)
下一篇
Day 30 -資料庫應用小程式 訂單顯示(內涵程式碼)
系列文
菜鳥大學生資料庫學習記30

1 則留言

1
shibainu274
iT邦新手 5 級 ‧ 2021-10-13 02:46:40

好棒 受教了

衛斯理 iT邦新手 5 級 ‧ 2021-10-13 23:29:42 檢舉

不客氣

我要留言

立即登入留言