版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、<p> Visual Basic Database ConnectivityAn Introductory Guide</p><p> 1. Create the VB .Net Application</p><p> Create a blank VB .Net solution in your account and add a newproject to the
2、 solution. This should automatically create a blankform for you. Also copy the Authors.mdb database file from the COMP315 library folder into your account. </p><p> 2. Using Data Adapters and Data Sets</
3、p><p> To manipulate data in an access database you need to use a data adapter to connect to the database and a data set to manipulate the data.</p><p> 3. Creating the Data Adapter</p>&l
4、t;p> To create a Data Adapter that contains the SQL statement you want to populate the dataset with, do the following: Click on the 'Data' tab in the tool box and then double click on the OleDbDataAdapter con
5、trol to add it into the form. The Data Adapter Configuration Wizard will automatically appear to help you configure the Data Adapter. </p><p> Figure 1. Location of the Data tab in the toolbox.</p>&
6、lt;p> Click on Next> to move to the next screen. Now the Data connection to use box should be blank so click on the New Connection button to configure the new connection. Select the Provider tab and then select Mi
7、crosoft Jet 4.0 OLE DB Provider and then click on Next>.</p><p> In section 1 where it asks for the database to use, click on the '…' button and then navigate to and select the Authors.mdb databa
8、se and click on the Open button.. Leave section 2 as it is. Your screen should look similar to this: </p><p> Figure 2. Completed Data Link Properties.</p><p> Click on the Test Connection but
9、ton and you should get a message saying 'Test Connection Succeeded'. If you don’t then check you have followed the instructions above. Click on OK to return to the Data Adapter Wizard and then click on the Next&g
10、t; button.</p><p> Make sure that Use SQL statements is selected and then click on the Next> button.</p><p> In the large text box type in the following SQL statement:</p><p>
11、 Select * from authors</p><p> Then click on the Next> button and then the Finish button. You may be asked if you want to include a password in the connection string, so just click on Don’t Include Passw
12、ord. You should now see a new Adapter object and a new Connection object.</p><p> 4. Creating the Dataset</p><p> The next step is to create a Dataset class based on the query you specified ab
13、ove. Make sure that your Data Adapter object is selected and at the bottom of the properties pane click on the Generate Dataset… link and you should see the window below appear.</p><p> Figure 3. The Genera
14、te Dataset window.</p><p> Select 'New' data set rather than 'Existing' data set and give it the name dsAuthors and make sure the 'Add This Dataset to the Designer' option is selecte
15、d. Click on the OK button to generate the authors dataset and you should see the Dataset object 'dsAuthors1' added to your project.</p><p> 5. Adding a Data Grid Control</p><p> Now yo
16、u need to add a data grid control to your form. Do this by selecting the Windows Forms tab in the tool box and then double click on the Data Grid control in the toolbox to add it to your form. Move it around and make it
17、bigger. </p><p> Go to the 'DataSource' property of the data grid and click on the down arrow and select dsAuthors1. For the 'DataMember' property select the Authors option. This has effecti
18、vely bound the data grid control to your authors dataset.</p><p> 6. Filling the Data Set With Information</p><p> Now run the application, what do you see? There is no data in the data grid!
19、What you need to do is to fill the dataset with the appropriate data. Stop the application running and then add a button and name it appropriately and give it the caption Display Data. Now create a click event procedure
20、for that button and in the click event procedure type in the following:</p><p> DsAuthors1.Clear()</p><p> OleDbDataAdapter1.Fill(DsAuthors1, "Authors")</p><p> The fir
21、st statement clears the dataset of previous information and the second statement uses the fill method of the data adapter to get the new data from the database. The first parameter of the Fill method is the dataset to fi
22、ll and the second parameter is the name of the table that you used when you set the DataMember property of the data grid control.</p><p> Now run your application again and when you click on your button you
23、 should see all the author information appear. If you click on the column names it will sort the data on that column. Pretty cool eh!</p><p> 7. Updating Changes Made in the Data Grid</p><p>
24、You can also change the information by clicking on a cell and then changing the data. Sort by last name and change the surname of 'Ann Dull' to 'Ann Exciting'. Close the application and run it again. What
25、 happened to the change you made to the surname? You should find that the surname is still 'Dull'. The reason why, is that the change was made to the data set but not to the database. The data set is like a tempo
26、rary table in the computers memory so any changes to it doesn't affect the data in t</p><p> You need to use the Update method of the Data Adapter to save any changes made to the dataset to the database
27、. To do this first add another button the form and give it the caption Save and then create a click event procedure for the button. Then type in the following statement in the click</p><p> event procedure:
28、</p><p> OleDbDataAdapter1.Update(DsAuthors1)</p><p> This statement calls the Update method of the Data Adapter and you need to specify the dataset to update as the parameter.</p><
29、p> Now run the application again and change the surname again and then click on the Save button. Close the application and run it again and you should see the change has been made to the database now. Try adding a ne
30、w row at thee bottom of the data grid using your name and make up data for the other attributes. Be careful to use the correct format for data otherwise you may get an error if the format of what you enter does not match
31、. Don’t forget to click on 'Save' to make the change to the databas</p><p> 8. Deleting Information using the Data Grid</p><p> Deleting a row from the table is also very easy. Just cl
32、ick in the area at the very beginning of the row before the first column to highlight the entire row. Now press the Delete key on your keyboard to delete the row and click on the Save button to save your changes to the d
33、atabase. In fact you don’t need to click on save after every change, just make all the changes to want to make and then click on the Save button at the very end and all changes will be saved to the database. Too Easy!<
34、;/p><p> 9. Filtering the Data Set</p><p> Now lets try filtering the data so that we only see the data we are interested in. We will use a combo box to allow the user to enter a value for the St
35、ate and then only show authors living in that state in the data grid. We will need a second data adapter to retrieve back all of the states in the database. Add another data adapter like you did for the first one and you
36、 should find that the database connection you used for the first one is already present so use that. When you type in your SQL s</p><p> Select distinct state from authors</p><p> Then click o
37、n the Advanced Options button and clear the option for 'Generate Insert, Update and Delete statements' as shown below:</p><p> Figure 4. Configuring the advanced options.</p><p> Then
38、click on Next> and Finish to create the second data adapter. At this point it would be better to rename this adapter so it easy to remember what the adapter is for so select the second data adapter object and in the p
39、roperties pane change the name to sdaState. Now create a second data set with the name dsState from your sdaState data adapter.</p><p> 10.Filling a Combo Box With Data From a Data Set</p><p>
40、 You are ready to create your combo box now so double click on the combo box control in the tool box to add it to your project and give it the name cboStates. Set the 'DataSource' property to dsState1 and the
41、9;DisplayMember' property to Authors.State, you will need to click the drop down arrow and then expand the Authors option to display the list of columns you can use. Set the 'DropDownStyle' property to DropDo
42、wnList and set the 'ValueMember' property of the combo box to Authors.State. This is </p><p> So we have the data set ready and the combo box ready, the last thing we need to do us to populate the c
43、ombo box automatically when the application runs. To do this we need to create a Form Load event procedure which will execute the statements in the procedure when the form is loaded. Just double click on an empty area of
44、 the form to create the form load event procedure</p><p> and then add the following statements: </p><p> DsState1.Clear()</p><p> sdaState.Fill(DsState1, "authors")<
45、;/p><p> Now run your application and you should see in the dropdown list of the combo box all of the states in the authors table.</p><p> Visual Basic數(shù)據(jù)庫連接介紹指南</p><p> 1.創(chuàng)造的VB.NET應(yīng)
46、用</p><p> 創(chuàng)建一個空白的VB.NET的解決方案,在您的帳戶,并添加新工程來解決這個問題。這應(yīng)該會自動創(chuàng)建一個空表給你。從comp315 Library文件夾到你的帳戶,還復(fù)制authors.mdb數(shù)據(jù)庫文件。</p><p> 2.利用數(shù)據(jù)適配器和數(shù)據(jù)集</p><p> 操縱數(shù)據(jù)在一個Access數(shù)據(jù)庫上,你需要使用一個數(shù)據(jù)適配器連接到數(shù)據(jù)庫和數(shù)
47、據(jù)集,以操縱數(shù)據(jù)。</p><p> 3.創(chuàng)建數(shù)據(jù)適配器 </p><p> 創(chuàng)建一個數(shù)據(jù)適配器包含SQL語句,你要填充數(shù)據(jù)集,做好以下工作:點擊'數(shù)據(jù)'標簽中的工具箱,然后雙擊該數(shù)據(jù)庫連接數(shù)據(jù)適配器控制它添加到表單。數(shù)據(jù)適配器配置向?qū)詣映霈F(xiàn),以幫助您配置數(shù)據(jù)適配器。 </p><p> 圖1.位置的數(shù)據(jù)統(tǒng)計表,在工具箱。</p&g
48、t;<p> 點擊未來>移動到下一個屏幕。現(xiàn)在,數(shù)據(jù)連接使用票房應(yīng)該空白,使點擊這個新的連接按鈕來配置新的連接。選擇供應(yīng)商標簽,然后選擇微軟提供的Jet 4.0 OLE DB,然后點擊下一步>。</p><p> 在第1條中,為所要求的數(shù)據(jù)庫來使用,點擊' … … '按鈕,然后瀏覽并選擇authors.mdb數(shù)據(jù)庫,并點擊打開按鈕..離開第2條,因為它是。你的屏幕應(yīng)該
49、看看類似這樣的: </p><p> 圖2.完成數(shù)據(jù)鏈路性能。</p><p> 點擊測試連接按鈕,你應(yīng)該看到一條信息,說'測試連接成功' 。如果你不那么清楚那你跟隨到以上指示。點擊OK返回到數(shù)據(jù)適配器向?qū)?,然后按一下該下一?gt; "按鈕。</p><p> 確保使用SQL語句是選定,然后按一下下一步> &qu
50、ot;按鈕。 </p><p> 在大文本框中鍵入以下SQL語句: </p><p> Select * from authors</p><p> 然后按一下下一步> "按鈕,然后點擊完成按鈕。你可能會問,在連接字符串如果你想包含一個密碼,所以只要點擊不包括密碼。你現(xiàn)在應(yīng)該看到一個新的適配器對象和一個新的Connection對象。 </
51、p><p><b> 4.創(chuàng)建數(shù)據(jù)集</b></p><p> 下一步該是創(chuàng)建基于查詢的數(shù)據(jù)集,您指定以上查詢。確保你的數(shù)據(jù)適配器對象選定,并在底部的屬性面板中點擊該能產(chǎn)生數(shù)據(jù)集的環(huán)節(jié),你應(yīng)該看到下面窗口出現(xiàn)。</p><p> 圖3.該數(shù)據(jù)集生成窗口。</p><p> 選擇'新'的數(shù)據(jù)集,而不是現(xiàn)
52、有的數(shù)據(jù)集,并賦予它的名字dsauthors并確保'添加此套數(shù)據(jù),以選中設(shè)計師該選項。點擊OK按鈕生成數(shù)據(jù)集的作者,你應(yīng)該看到數(shù)據(jù)集對象' dsauthors1 '添加到您的項目。</p><p> 5.添加一個數(shù)據(jù)網(wǎng)格控制</p><p> 現(xiàn)在,你需要添加一個數(shù)據(jù)網(wǎng)格控制到表單中。做到這一點可以通過選擇窗口表格標簽中的工具箱,然后雙擊該數(shù)據(jù)網(wǎng)格控制在工具箱以
53、便將它添加到表單中。移到表單,并使它有更大發(fā)展。</p><p> 除去數(shù)據(jù)源參數(shù)的安全數(shù)據(jù)網(wǎng)格,并點擊向下的箭頭,并選擇dsauthors1 。為選擇 數(shù)據(jù)成員參數(shù)專責的作者。這有力地約束數(shù)據(jù)網(wǎng)格控制你的作者數(shù)據(jù)。</p><p><b> 6.填充數(shù)據(jù)集信息</b></p><p> 現(xiàn)在運行的應(yīng)用,你怎么看?在數(shù)據(jù)網(wǎng)格有沒有數(shù)據(jù)!你
54、需要做的是填補適當?shù)臄?shù)據(jù)。停止運行的應(yīng)用,然后添加一個按鈕和得當名稱,并賦予它屏幕顯示數(shù)據(jù)?,F(xiàn)在創(chuàng)造一個點擊事件的程序,那個按鈕,并在點擊事件程序類型,在以下方面:</p><p> DsAuthors1.Clear()</p><p> OleDbDataAdapter1.Fill(DsAuthors1, "Authors")</p><p&g
55、t; 第一項聲明擦亮了數(shù)據(jù)集的前信息和第二次聲明,使用充填采礦法的數(shù)據(jù)適配器,從資料庫中以獲取新的資料。第一個參數(shù)的填寫方法是將數(shù)據(jù)填補,第二個參數(shù)是表的名稱,你用的時候,你定數(shù)據(jù)成員參數(shù)的安全數(shù)據(jù)網(wǎng)格控制。</p><p> 現(xiàn)在運行你的應(yīng)用程序,再當你點擊你的按鈕,你應(yīng)該可以看到所有的作者信息出現(xiàn)。如果你點擊這個欄目的名字,將上欄進行數(shù)據(jù)排序。蠻不錯嗯!</p><p> 7.在
56、數(shù)據(jù)網(wǎng)格更新的變化</p><p> 您也可以通過點擊一個基本存儲單元更改信息,然后再改變數(shù)據(jù)。按照姓名排序和改變姓名 'Ann Dull' 到 'Ann Exciting'。關(guān)閉該應(yīng)用程序并運行它。所發(fā)生的事情,以改變你的姓?你應(yīng)該發(fā)現(xiàn)姓仍然是'枯燥'。原因是,記錄改變以數(shù)據(jù)集,而不是數(shù)據(jù)庫。該數(shù)據(jù)集該像一個臨時表,在電腦記憶體里,所以它任何改變并不影響數(shù)據(jù)庫中
57、的實際表中的Access數(shù)據(jù)庫。</p><p> 你需要使用更新的方法數(shù)據(jù)適配器,以節(jié)省所做的任何更改數(shù)據(jù)集到數(shù)據(jù)庫。要做到這一點首先放入另一個按鈕的布局,并賦予它字幕保存,然后創(chuàng)造一個點擊事件的程序按鈕。然后鍵入以下聲明中點擊活動程序:</p><p> oledbdataadapter1.update(dsauthors1)</p><p> 這項聲明呼
58、吁更新方法的數(shù)據(jù)適配器,你需要指定數(shù)據(jù)集,以更新作為參數(shù)。</p><p> 現(xiàn)在運行的應(yīng)用,并再次改變姓氏,然后再次點擊保存按鈕。關(guān)閉該應(yīng)用程序并讓它再次運行,你應(yīng)該看到它已經(jīng)向數(shù)據(jù)庫的變化。嘗試加入一個新的排在禰底部的數(shù)據(jù)網(wǎng)格用你的名字和彌補數(shù)據(jù),為其它屬性。注意為數(shù)據(jù)使用正確的格式,否則,如果格式要求是什么,你輸入不匹配,你可能會得到一個錯誤。不要忘了點擊'保存',以改變數(shù)據(jù)庫。</
59、p><p> 8.刪除信息使用數(shù)據(jù)網(wǎng)格</p><p> 從表中刪去一排也很容易。只需按一下該地區(qū)在開始的前行的第一欄,以突出整個排?,F(xiàn)在按下刪除鍵,即可刪除該行并點擊保存按鈕保存更改到數(shù)據(jù)庫。事實上,你不用每一次變化后都點擊保存,也希望只是盡一切改變,然后點擊保存按鈕,在最后一刻,所有的改變將被保存到數(shù)據(jù)庫。太容易了!</p><p><b> 9.過
60、濾數(shù)據(jù)集</b></p><p> 現(xiàn)在試圖過濾數(shù)據(jù),我們感興趣的是讓我們看到的只是數(shù)據(jù),我們將使用一個組合框,讓用戶輸入一個值的狀態(tài),然后,只能說明作者在數(shù)據(jù)網(wǎng)格的該狀態(tài)。我們還需要有第二個數(shù)據(jù)適配器取回回所有的狀態(tài)在數(shù)據(jù)庫中。加上另一個數(shù)據(jù)適配器一樣,你做了第一次,你應(yīng)該發(fā)現(xiàn)數(shù)據(jù)庫連接,你用第一種是已經(jīng)存在,所以利用這一點。當你輸入你的SQL語句,輸入以下內(nèi)容:</p><p
61、> 選擇從表沖不同的狀態(tài),</p><p> 然后點擊高級選項按鈕,并明確了選擇“生成插入,更新和刪除報表” ,如下所示:</p><p> 圖4.配置高級選項。</p><p> 然后點擊下一步> ,并整理,以創(chuàng)造第二個數(shù)據(jù)適配器。在這一點上,為這個適配器將更好地改名,所以容易被記住的適配器,所以選擇第二個數(shù)據(jù)適配器對象,并在屬性面板中的名稱更
62、改為sdastate?,F(xiàn)在,創(chuàng)建第二個數(shù)據(jù)集,該人的姓名dsstate 在你的sdastate數(shù)據(jù)適配器中。</p><p> 10.從一個數(shù)據(jù)集填補了組合框中的數(shù)據(jù)</p><p> 你已準備好,創(chuàng)造你的組合框中,現(xiàn)在這么在工具箱控制雙擊該組合框中以便將它添加到你的項目,并賦予它的名字cbostates 。定'的DataSource '參數(shù)dsstate1和'
63、 displaymember '參數(shù)authors.state ,你將需要點擊下拉箭頭,然后再擴大作者選項顯示名單欄目,你可以使用。定' dropdownstyle '參數(shù)dropdownlist ,并設(shè)定' valuemember '參數(shù)的安全組合框中,以authors.state 。這該是用這樣的實際價值,是用來當一個項目是在選定的名單中。</p><p> 因此,我
64、們有數(shù)據(jù)集的準備,并在組合框中準備好,最后我們需要做的,我們力求普及組合框中時自動運行的應(yīng)用。要做到這一點,我們需要創(chuàng)造一種形式負荷事件的程序,將執(zhí)行報表,在程序當形式加載。剛剛雙擊一個空白區(qū)的事件創(chuàng)造了形式負荷事件程序然后添加以下聲明:</p><p> DsState1.Clear()</p><p> sdaState.Fill(DsState1, "authors&q
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外文翻譯-數(shù)據(jù)庫管理系統(tǒng)介紹
- 外文翻譯---visual basic編程語言
- 外文翻譯----visual basic編程語言
- 數(shù)據(jù)庫管理系統(tǒng)的介紹外文翻譯
- 數(shù)據(jù)庫畢業(yè)設(shè)計外文翻譯--數(shù)據(jù)庫管理系統(tǒng)的介紹
- visual basic畢業(yè)設(shè)計外文翻譯
- 外文翻譯---visual basic 語言與算法
- 外文翻譯---visual basic 語言與算法
- 外文翻譯--Visual Basic編程語言概述.doc
- vb外文翻譯--- visual basic語言和算術(shù)
- 外文翻譯--Visual Basic編程語言概述.doc
- 外文翻譯--Visual Basic編程語言概述.doc
- 外文文獻翻譯--數(shù)據(jù)庫管理系統(tǒng)的介紹
- 外文翻譯----數(shù)據(jù)庫和數(shù)據(jù)庫系統(tǒng)
- 外文翻譯--Visual Basic編程語言概述.doc
- 如何鎖定 visual sourcesafe 數(shù)據(jù)庫
- 數(shù)據(jù)庫設(shè)計外文翻譯
- sql數(shù)據(jù)庫外文翻譯
- 數(shù)據(jù)庫設(shè)計外文翻譯
- 數(shù)據(jù)庫管理-外文翻譯
評論
0/150
提交評論