




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 昆明學(xué)院《現(xiàn)代辦公技能訓(xùn)練A》2023-2024學(xué)年第二學(xué)期期末試卷
- 2025年非接觸溫度計(jì)項(xiàng)目合作計(jì)劃書
- 蘇州城市學(xué)院《場(chǎng)景特效》2023-2024學(xué)年第二學(xué)期期末試卷
- 噴槍及類似器具項(xiàng)目效益評(píng)估報(bào)告
- 全國川教版信息技術(shù)八年級(jí)上冊(cè)第9課《編輯工作表》教學(xué)設(shè)計(jì)
- 桂林師范高等??茖W(xué)?!稊?shù)字繪畫技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 農(nóng)村打井簡易合同范本
- 揚(yáng)州大學(xué)《展具設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 上海立達(dá)學(xué)院《食品營養(yǎng)與衛(wèi)生管理》2023-2024學(xué)年第二學(xué)期期末試卷
- 河南2024年河南信陽師范大學(xué)招聘專職輔導(dǎo)員30人筆試歷年參考題庫附帶答案詳解
- 功率測(cè)量模塊的軟件設(shè)計(jì)方案與實(shí)現(xiàn)
- 中考英語高頻單詞專項(xiàng)訓(xùn)練題配套答案
- 火龍罐療法經(jīng)典課件
- 核島通風(fēng)系統(tǒng)介紹
- 我見證改革開放三十年課件
- xxxx智能化工程施工進(jìn)度計(jì)劃表
- 供應(yīng)商來料包裝運(yùn)輸存儲(chǔ)規(guī)范
- 搭橋術(shù)后護(hù)理查房
- 人工智能概論P(yáng)PT全套完整教學(xué)課件
- 第一章-運(yùn)動(dòng)選材概述課件
- 藏式建筑簡介優(yōu)質(zhì)課件
評(píng)論
0/150
提交評(píng)論