[讀書筆記] Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 第三十五章

  • 1401
  • 0

閱讀Stephens' C#教材第三十五章筆記 介紹如何建立資料庫連接,並簡單顯示出資料庫內的紀錄資料

 

Chapter 35 Programming Databases, Part 1
 
資料庫程式設計是一個非常大的題目,不可能全部在這裡討論,Visual Studio提供工具讓一些簡單的資料庫操作過程變得非常簡單。
 
本章將介紹如何做出一個簡單的資料庫應用程式,將會學到如何連結到資料庫、載入資料、讓使用者巡覽紀錄,並儲存資料。類似的資料請參考微軟網頁
 
連接資料庫
1.從[資料]選單點選[加入新資料來源]會出現下圖,按[下一步]
 
2.出現的畫面中要你選擇使用[資料集]或是[實體資料模型],點選[資料集],按[下一步]
 
3.出現的畫面中可以選擇已有的連結,這裡選擇[新增連結]
 
4.選擇資料來源的類型,因為要使用的是Access的資料庫檔案,所以選第一個,按[繼續]。
 
5.選擇本章範例所附帶的people.mdb,並在測試連接成功後,按下[確定]。
 
6.回到選擇連結的畫面,按[下一步]
 
7.Visual Studio詢問是否要將people.mdb複製一份到程式內來,作者建議按[是],將資料庫新增的專案中。
 
8.是否將連結字串儲存到應用程式組態檔中,保持勾選,按[下一步]
 
9.選擇要使用的資料庫物件,將PeopleNames勾選,裡面的三個欄位都會被選擇,按[完成]。
 
在Grid物件中顯示紀錄資料
1.要在Grid物件中顯示資料,第一步需要開啟資料源,然後叫出專案的資料來源視窗(從[資料]選單點選[顯示資料來源])
 
2.將PeopleNames從資料來源以拖曳的方式拉到表單上,然後放開滑鼠左鍵。
 
3.Visual Studio會自動增加許多物件到專案中,比如表單上會出現Grid物件,以及上方的巡覽列,下方五個不顯示的物件,共有七個分別簡單介紹如下:
DataGridView:顯示資料的物件。
Dataset:運行在記憶體中保存資料的資料集。
BindingSource:封裝有資料源,負責資料源與表單物件間的連結。
Data adapter:負責提供方法讓資料在資料庫與資料集間移動。
Table adapter manager:協調Data adapter對於資料的移動。
Binging navigator:提供巡覽服務給表單上的物件,讓資料可透過上一頁下一頁按鈕,改變物件的顯示資料內容。
 
 
4.執行程式即可巡覽people.mdb資料庫裡PeopleNames資料表的內容。
 
5.如果要讓程式可以在Grid中輸入資料並真正儲存到people.mdb資料庫中,需要增加下列程式碼:

        // Check for unsaved changes.
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.peopleDataSet.HasChanges())
            {
                // Make the user confirm.
                DialogResult result = MessageBox.Show(
                    "Do you want to save changes before closing?",
                    "Save Changes?",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question);
                if (result == DialogResult.Cancel)
                {
                    // Cancel the close.
                    e.Cancel = true;
                }
                else if (result == DialogResult.Yes)
                {
                    // Save the changes.
                    peopleNamesTableAdapter.Update(peopleDataSet);

                    // Make sure the save worked.
                    // If we still have unsaved changes, cancel.
                    e.Cancel = (this.peopleDataSet.HasChanges());
                }

                // Else the user doesn't want to save the changes so just keep going.
            }
        }

 

以上程式可參考範例中的PeopleGrid程式

 
若想在在Grid物件中一次顯示一筆紀錄資料
1.在資料來源中對PeopleNames選擇[詳細資料],然後再拖曳到表單上即可出現下圖右側畫面:
 
 
2..如果要讓程式可以在Grid中輸入資料並真正儲存到people.mdb資料庫中,需要增加下列程式碼:

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'peopleDataSet.PeopleNames' table. You can move, or remove it, as needed.
            this.peopleNamesTableAdapter.Fill(this.peopleDataSet.PeopleNames);

            // Enable the Add New and Delete buttons.
            this.bindingNavigatorDeleteItem.Enabled = true;
            this.bindingNavigatorAddNewItem.Enabled = true;
        }

 

FormClosing事件中要額外增加前兩行程式


        // Check for unsaved changes.
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Validate();
            this.peopleNamesBindingSource.EndEdit();

            if (this.peopleDataSet.HasChanges())
            {
                // Make the user confirm.
                DialogResult result = MessageBox.Show(
                    "Do you want to save changes before closing?",
                    "Save Changes?",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question);
                if (result == DialogResult.Cancel)
                {
                    // Cancel the close.
                    e.Cancel = true;
                }
                else if (result == DialogResult.Yes)
                {
                    // Save the changes.
                    peopleNamesTableAdapter.Update(peopleDataSet);

                    // Make sure the save worked.
                    // If we still have unsaved changes, cancel.
                    e.Cancel = (this.peopleDataSet.HasChanges());
                }

                // Else the user doesn't want to save
                // the changes so just keep going.
            }
        }

 

以上程式可參考範例中的PeopleField程式

 
TRY IT中示範SelectColors程式如何一步一步建立出可連線到Contacts.mdb的,與上面PeopleGrid程式類似的步驟。