




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、DatabaserIntroduksjon til DatabaserResten av foilene fra kap 10.1 som vi gr gjennom p neste forelesningChapter 10 Database Management10.1 An Introduction to Databases10.1 An Introduction to DatabasesDatabase ExplorerAccessing a Database with a Data TableSample Table Cities TableSample Table Countrie
2、s TableDatabase TerminologyA table is a rectangular array of data. Each column of the table, called a field, contains the same type of information.Each row, called a record, contains all the information about one entry in the database. Database Management Software (DBMS)Used to create databasesDatab
3、ases can contain one or more related tablesExamples of DBMS include Access and OracleSe databasen fra Visual BasicDatabase ExplorerThe Standard and Professional editions of Visual Basic contain Server Explorer that also allows the programmer to view information located on other computers.We will foc
4、us on Database Explorer. However, with slight modifications, our discussion applies to Server Explorer.Using the Database Explorerside 513Click on Database Explorer from the View Menu.The Explorer will appear on the left side of the screen.Right-click on “Data Connections”, and select “Add Connectio
5、n”.Set the Data Source to “Microsoft Access Database File.”Click on the “Browse ” button and select the file MEGACITIES.MDB from the folder ProgramsCh10MajorDatabases, and press Open.Clear the contents of the “User name” text box.Database Explorer continuedPress the Test Connection button. The messa
6、ge box stating “Test Connection Succeeded” will appear. Press the OK button on that message box, and then press the OK button on the Data Link Properties box.An icon should appear in Database Explorer. Click on the + sign to the left of the icon to expand this entry. four subentries will appear: Tab
7、les, Views, and Stored Procedures, and Functions.Expand the Tables entry to reveal the subentries, the tables Cities and Countries. Expand an entry to reveal the fields of the table. (See next slide: Figure 10.1)Double-click on a table to show the table in a grid. (See slide two pages ahead: Figure
8、10.2)Figure 10.1 Database ExplorerFigure 10.2 The Cities TableVi lager et VB program som henter data fra en databaseBruker et DataTable object i VBDataTable object er en tabellVi har sett p tabeller med en kolonne og mange rekkerF.eks.: Dim biler() As BilBMW, 325, 1999VW, Caravele, 1997Toyoa, Landcr
9、uiser, 2000. henter data fra en databaseTabeller ha:mange kolonner og mange rekker (todimensjonale)en egen tabell med todimensjonale tabeller! (tredimensjonale)osv. i prinsippet i det uendelige!I INF150 ser programmerer skal vi beherske endimensjonale og kjenne til todimensjonaleTabeller med flere e
10、nn to dimensjoner benyttes sjeldenTodimensjonale tabeller(7.5 Two-dimensional Arrays p.377)Hittil har vi sett p endimensjonale tabeller hvor hvert felt kan inneholde:Enkle verdierStrukturerAlle felt m vre av samme typeN ser vi p todimensjonale tabellerOgs her m alle felt m vre av samme typeF.eks. Bi
11、lannonseMerke, Modell, Arsmodell, AnnonseSattInn, AntVisningerEksempel p todimmensjonal tabellBilannonseMerkeModellArsmodellAnnonseSattInnAntVisningerBMW325199917.11.20069VWCaravelle199711.4.200618ToyotaLandcruiser200020.8.200628Oppgave: Indiker hva som er et “Field” og en “Record” Todimensjonal tab
12、ell i Visual BasicDeklarerer tabell med 10 rekker og 5 kolonner:Dim bilannonse(9, 4) As String Legger inn verdier i kolonnene i rekke 0:bilannonse(0,0) = “BMW”bilannonse(0,1) = “325”bilannonse(0,2) = “1999”bilannonse(0,3) = “17.11.2006”bilannonse(0,4) = “9” Legger inn verdier i kolonnene i rekke 1:b
13、ilannonse(1,0) = “VW”MsgBox(bilannonse.Length)Alle feltene var av type StringDet er vanlig at databaser lagrer alle data som StringVi kan gi tilleggsopplysninger til databasen om hvilken type teksten representerer slik at de som leser databasen kan omforme til rett typeVi legger tabellen inn i Micro
14、soft Access og leser den inn i et VB programStart Access .Opprett BILANNONSER.MDBLag annonser-tabellLegg inn de tre annonseneStart Visual Basicpne for tilgang til DataObject .“Data Table Object”Et “DataTable object” er et ferdig Visual Basic objekt som inneholder en kopi av innholdet i en databaseta
15、bell.En DataTable ligner en vanlig todimensjonal tabell i VB med kolonner og rekker.pne for tilgang til DataObjectVB har mange ferdige objekter og kontroller.Koden til alle er svrt omfattende, s det er bare de vanligste som er tatt med som standard.Vi m derfor gi beskjed dersom vi vil benytte det so
16、m ikke er standard, s som DataObjectGi beskjed om ta med koden til DataObject (se de neste slides)I VB-editoren: Ta med koden til DataObject, denne finnes i System.Data.dll and System.Xml.dll (se side 521:)a) Click on Project in the Menu bar.b) Click on Add Reference in the drop-down menu. To invoke
17、 the “Add Reference” dialog box.c) Make sure the .NET tab is selected.d) Click on System.Data. Hold down the Ctrl key and click on System.Xml.e) Press the OK button.I koden vrI kodevinduet, helt p toppen:Imports System.DataPublic Class Form1 .End ClassConnecting with a DataTableside 521Dim dt As New
18、 DataTable()Dim connStr As String = _ Provider=Microsoft.Jet.OLEDB.4.0; & _ Data Source=MEGACITIES.MDBDim sqlStr As String = SELECT * FROM CitiesDim dataAdapter As New _ OleDb.OleDbDataAdapter(sqlStr, connStr)dataAdapter.Fill(dt)dataAdapter.Dispose()Linje 1 forrige sideDim dt As New DataTable()Dekla
19、rerer dt som en variable av type DataTableLinje 2Dim connStr As String = _ Provider=Microsoft.Jet.OLEDB.4.0; & _ Data Source=MEGACITIES.MDBAngir DatabasedriverenFilespec til databasefilenforutsetter at filen ligger i bin/debug katalogenLinje 3Dim sqlStr As String = SELECT * FROM CitiesSier hvilken t
20、abell data skal hentes fraSQL: Standard Query LanguageLinje 4Dim dataAdapter As New _ OleDb.OleDbDataAdapter(sqlStr, connStr)DB er p disken mens DataTable er i minnetAdapter inneholder kode som omformer vr foresprsel gitt i sqlstr til kode tilpasset databasen gitt i connStrLinje 5 og 6dataAdapter.Fi
21、ll(dt)dataAdapter.Dispose()Bruker DataAdapter til hente data fra DB til tabellen i dtDispose() kalles nr vi ikke har bruk for tilkoblingen mer.Kode for hente fra DBImports System.DataPublic Class Form1 Dim dt As New DataTablePrivate Sub btnTest_Click(.) Handles btnTest.Click Dim connStr As String =
22、Provider=Microsoft.Jet.OLEDB.4.0; & _ Data Source=BILANNONSER.MDB Dim sqlStr As String = SELECT * FROM Annonser Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr) dataAdapter.Fill(dt) dataAdapter.Dispose() . se neste side Kode for hente fra DB lstVis.Items.Add(”Ant. rekker: ” & dt.Rows.C
23、ount) lstVis.Items.Add(”Ant. kolonner: ” & dt.Columns.Count) lstVis.Items.Add(”Rekke 0 kolonne 0: ” & dt.Rows(0)(0) lstVis.Items.Add(”Rekke 0 kolonne modell: ” dt.Rows(0)(modell)End SubDim fmtStr As String = 0,-151,-152,-53,114,5”For i As Integer = 0 To dt.Rows.Count - 1 lstVis.Items.Add(String.Form
24、at(fmtStr, dt.Rows(i)(0), dt.Rows(i)(1),_ dt.Rows(i)(2), dt.Rows(i)(3), dt.Rows(i)(4)NextUtskrift til listbox formateres:Tabell med 3 rekker og 5 kolonner:Dim bilannonse(2, 4) As StringKopierer fra DataTable, dt:bilannonse(0, 0) = dt.Rows(0)(0) . Kode for hente fra DataObject til vr egen todimenjona
25、le tabellProperties of the DataTableside 522After the six lines of code are executed, the number of records in the table is given by dt.Rows.CountThe number of columns in the table is given by dt.Columns.CountThe records are numbered 0 through dt.Rows.Count 1The fields are numbered 0 through dt.Colu
26、mns.Count 1.The name of the jth field is given by dt.Columns(j)The entry in the jth field of the ith record is dt.Rows(i)(j)The entry in the specified field of the ith record is dt.Rows(i)(fieldName)Example 1: FormDisplay one record at a time from the Cities table.Example 1: Partial CodeDim dt As Ne
27、w DataTable()Dim rowIndex As Integer = 0Private Sub frmCities_Load(.) Handles _ MyBase.Load (Last five statements of boilerplate) UpdateTextBoxes()End SubSub UpdateTextBoxes() Display contents of row specified by rowIndex variable txtCity.Text = CStr(dt.Rows(rowIndex)(city) txtCountry.Text = CStr(dt
28、.Rows(rowIndex)(country) txtPop2005.Text = CStr(dt.Rows(rowIndex)(pop2005) txtPop2015.Text = CStr(dt.Rows(rowIndex)(pop2015)End SubExample 1: Partial Code cont.Private Sub btnNext_Click(.) Handles btnNext.Click Show the next record if current one is not the last If (rowIndex 0) Then rowIndex = rowIn
29、dex - 1 UpdateTextBoxes() End IfEnd SubExample 1: Partial Code cont.Private Sub btnFind_Click(.) Handles btnFind.Click Dim cityName As String Dim cityFound As Boolean = False cityName =InputBox(Enter name of city to search for.) For i As Integer = 0 To (dt.Rows.Count - 1) If CStr(dt.Rows(i)(city) =
30、cityName Then cityFound = True rowIndex = i UpdateTextBoxes() End If Next If (Not cityFound) Then MsgBox(Cannot find requested city,0,Not in Table) End IfEnd SubExample 1: OutputExample 2: FormDisplay Cities table along with percentage growth.Example 2: CodePrivate Sub btnShow_Click(.) Handles btnSh
31、ow.Click Dim fmtStr As String= 0,-151,-102,7:N13,7:N14,7:P0 Dim percentIncrease As Double (Six statements of boilerplate) lstDisplay.Items.Add(String.Format(fmtStr, CITY, _ COUNTRY, 2005, 2015, INCR.) For i As Integer = 0 To dt.Rows.Count - 1 percentIncrease = (CDbl(dt.Rows(i)(pop2015) - _ CDbl(dt.Rows(i)(pop2005) / CDbl(dt.Rows(i)(pop2005)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 第五單元寫作:學寫游記++課件-2024-2025學年八年級語文下冊同步備課精講課件(統(tǒng)編版)
- 瑪莎法則患者安全2025
- 快樂體育在大學體育教學中的實施研究
- 物理因子試題及答案詳解
- 2025年河南省駐馬店市中考三模語文試題(含答案)
- 2025公立中學教師勞動合同
- 2025年中國水暖管道零件制造行業(yè)市場前景預測及投資價值評估分析報告
- 2025年中國室內(nèi)自行車滾輪行業(yè)市場前景預測及投資價值評估分析報告
- G新基建省域智慧醫(yī)療協(xié)作平臺解決方案
- 2025屆高考物理大一輪復習課件 第十二章 第69課時 專題強化:電磁感應中的動力學和能量問題
- 注塑領班工作總結
- 全面指南:2024年醫(yī)學整形美容醫(yī)院員工手冊
- 2025年中國經(jīng)濟信息社福建分公司招聘筆試參考題庫含答案解析
- 2025年度食用菌產(chǎn)業(yè)園區(qū)公共設施運營管理合同3篇
- 《費孝通-鄉(xiāng)土中國》差序格局
- 2023-2024學年天津市和平區(qū)八年級(下)期末數(shù)學試卷(含答案)
- 2021去遠方上海研學旅行方案申請及綜合反思表
- 2025屆北京市人大學附屬中學高三下學期一模考試英語試題含解析
- 二級等保標準
- 藥棒穴位按摩技術
- 行政案例分析-終結性考核-國開(SC)-參考資料
評論
0/150
提交評論