




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
數(shù)據(jù)庫用戶接口有能力的數(shù)據(jù)庫應用設計者和創(chuàng)建者總是被當作公司或者商業(yè)的巨大資產(chǎn),并且應當這樣。在用戶需要通過局域網(wǎng)、廣域網(wǎng)或者因特網(wǎng)交互數(shù)據(jù)時,這些人能夠解決許多關(guān)鍵性的問題。如果你已經(jīng)構(gòu)建起數(shù)據(jù)庫系統(tǒng),剩下的就只是如何構(gòu)建接口來讓用戶與這些系統(tǒng)進行交互。還有一些其它方面你需要了解,例如:設計一個適合三層構(gòu)架的系統(tǒng),這些將在以后討論。當今的銀行、商店以及許多企業(yè)的日常工作都是基于一定數(shù)據(jù)庫系統(tǒng)來完成。如果沒有數(shù)據(jù)庫系統(tǒng),它們將不能有效地運轉(zhuǎn)。這就是開發(fā)者發(fā)揮他們才能和不可或缺作用的地方。為了能夠綁定一個用戶接口,如果可以的話,也可以綁定其他相關(guān)商業(yè)部分,數(shù)據(jù)源是很有價值的一個資本。想想最近一次你使用應用程序,也許是一本雇員電話本、顧客數(shù)據(jù)庫、清單程序,或者是財政程序,想想它們?nèi)绾问占瘮?shù)據(jù)并且用一種友好的方式展現(xiàn)給你。一旦能夠建立這些形式的應用,就擁有了能夠很容易地進行信息處理的潛能,能夠給整個公司帶來利益,這就是為什么程序員成為如此珍貴資本的原因。當今的許多公司都雇傭一個個人或者群體專門進行已存在的數(shù)據(jù)庫的管理一即就是數(shù)據(jù)庫管理員(DBAs)-并且擁有一組單獨的開發(fā)人員,他們的任務是進行自己應用程序的創(chuàng)建。但是,作為數(shù)據(jù)庫開發(fā)者的你常常兼有這兩方面的工作,所以你需要深入了解這兩個任務。數(shù)據(jù)庫管理員的職責包括:□數(shù)據(jù)庫構(gòu)架知識和如何高效地運用和存儲信息。□使數(shù)據(jù)庫能夠正常運行的能力?!醵床旃驹诔砷L和擴大中的需求變化的能力一現(xiàn)在的數(shù)據(jù)庫框架能否處理十倍甚至百倍數(shù)量級的信息而保持高效率?□當進行用戶接口設計時有一套邏輯方法,屏幕按一種用戶能夠理解的方式進行組織一例如相關(guān)組進行控制,比如用戶和地址字段。□與用戶緊密地工作來設計這些系統(tǒng)的能力。在進行客戶一服務器應用程序時這些將會有幫助。當我們開發(fā)數(shù)據(jù)庫時我們需要考慮諸如:效率、靈活性、處理多用戶、數(shù)據(jù)鎖定、數(shù)據(jù)增長、完善性和性能等。開發(fā)可戶端應用程序的人員需要像考慮程序性能和效率一樣考慮用戶接口界面。擁有一個聰明的想法:知道用戶將會如何與數(shù)據(jù)庫系統(tǒng)進行交互將會幫你決定如何設計它。用戶在運行任務時總是按一定的順序運行特定的程序,某些任務比其他的要用的多。例如,如果你在設計一套儲備清單程序,你也許想讓你的系統(tǒng)在開啟時顯示能讓用戶更改清單的界面,而不是添加新產(chǎn)品行。在這章中,我們將來看看為一個SQL數(shù)據(jù)源建立一個基本用戶接口的重要基本。我們也將看看當你建立一些更老練的數(shù)據(jù)庫應用時所應該應用的一些好的實踐。下面就是我們?yōu)榇四康亩采w的主題:一個用來訪問數(shù)據(jù)庫的簡單用戶接口怎樣填充一個DataGrid□運用向?qū)А跄缓螽a(chǎn)生的代碼一般用戶接口設計的好的實踐1用戶接口既然你已經(jīng)學會了怎樣運用合適的SQL語句來設計數(shù)據(jù)庫并且更新數(shù)據(jù)庫里的信息。我們不能等待我們的用戶去開發(fā)一套數(shù)據(jù)庫管理工具并且像這樣來操縱數(shù)據(jù),因此,我們下一步就是要創(chuàng)造某種途徑來讓用戶去訪問信息。這就是用戶接口的作用一提供一種用戶和數(shù)據(jù)庫的通信手段。通過用戶接口,用戶可以與數(shù)據(jù)交互并且操縱它來完成一定的任務,比如:查看、添加、更新或者刪除細節(jié)。用戶接口和數(shù)據(jù)庫親密地進行工作,并且正確結(jié)合,用戶能夠不用任何特殊的技術(shù)知識而處理大量的信息。需要某中思考過程或者方法來為接口提出一種好的設計:我們想建立一個一致并且和諧的界面以便這個接口對將要用它的用戶來說是容易接近的。我們應該知道現(xiàn)存的數(shù)據(jù)邏輯,當讓用戶進行像添加和刪除信息時保證這些動作是正確的,防止用戶不經(jīng)意的操縱數(shù)據(jù)造成一些對企業(yè)來說不可恢復的損失。安全性和數(shù)據(jù)完整性很重要,因為它們提供了機制來保證用戶不能調(diào)整他們自己財產(chǎn)的平衡,修改他們自己的獎金,或者查看甚至修改其他用戶的信息。我們需要一種方法來隱藏幕后的制衡。這些中的許多都可通過好的框架設計來完成,來造成一種能夠訪問、使之有效并且保護這個信息,或者通過框架后的代碼或者讓這些框架去調(diào)用另一個對象。還有一種可能性是一個惡意的用戶可能會完全饒過這些模型,并且嘗試著去直接訪問數(shù)據(jù)庫,因此嚴密的安全是非常重要的。為了達到這個目的,我們的模型應該制作成一個帶有保密用戶ID和密碼的數(shù)據(jù)庫,我們能通過存儲過程隱藏我們的制衡信息一也就是存儲在數(shù)據(jù)庫服務器端的程序。這些存儲過程能履行一些基于已通過參數(shù)的標準,并且只有當正確的安全信息通過時才返回信息。所有的這些都是通過商業(yè)規(guī)則來完成的,這些規(guī)則提出這樣的要求:數(shù)據(jù)在特殊的與我們的生意有關(guān)時如何維護。既然我們已對與用戶接口相關(guān)的問題了解了一部分,我們可以繼續(xù)來看看我們怎樣在VisualBasic.NET里創(chuàng)建一個接口。這個應用程序的基本要素是窗體框架,它們可以被看作是一個等待被畫入的白布。我們可以在這個框架中放置控件來為我們的接口設計一種有邏輯性的界面,并且為用戶提供一種自然而簡單的途徑來讓用戶與數(shù)據(jù)庫交互。2ADO.NET概述在我們開始創(chuàng)建我們的應用程序之前我們需要來學習一下我們的應用程序?qū)⒁玫降囊恍〢DO.NET基本要素和向?qū)АN覀儗⒃谙乱徽轮懈敿毜孛枋鯝DO.NET,因此,現(xiàn)在我們將僅學習一下基本來使我們通過這個簡單的數(shù)據(jù)庫工程。ADO.NET提供給我們一種收集數(shù)據(jù)和信息并且通過一個用戶接口展示數(shù)據(jù)的方法。通過運用一些要素,我們能夠聯(lián)系上各種數(shù)據(jù)源然后創(chuàng)建一個訪問數(shù)據(jù)庫的用戶接口。我們需要四個部分來創(chuàng)建我們的ADO.NET工程。一個數(shù)據(jù)源一真正的數(shù)據(jù)被存儲的地方,我們的數(shù)據(jù)庫一個Connection對象一來將我們聯(lián)系到我們的數(shù)據(jù)庫一個DataAdapter對象一來創(chuàng)建一種讀取和向數(shù)據(jù)庫中寫入的機制一個數(shù)據(jù)集對象一它將包含我們將要用到的表首先,我們需要一個與數(shù)據(jù)源的連接,被一個Connection對象所提供。該對象要求一定的信息來與數(shù)據(jù)庫連接。Connection對象被DataAdapter對象所調(diào)用,DataAdapter對象處理命令來對數(shù)據(jù)源進行選擇、更新、插入和刪除數(shù)據(jù)。最后,我們有一個DataSet來裝入我們的表并且它通過DataAdapter填充自己并且對數(shù)據(jù)源中的數(shù)據(jù)進行更新。一、數(shù)據(jù)源數(shù)據(jù)源是描述提供給我們數(shù)據(jù)的任意信息集合的規(guī)定。它可能采取數(shù)據(jù)庫,XML文件的形式,報表,甚至一個文本或二進制文件。它只采取一兩個代碼行使我們改變我們連接的數(shù)據(jù)源。Windows環(huán)境提供我們公共類用于我們的程序用相同的代碼與不同的數(shù)據(jù)源通信。二、數(shù)據(jù)連接我們連接數(shù)據(jù)庫的第一件事是數(shù)據(jù)連接對象。它引用二種形式-SqlConnection或OleDbConnection對象。就像我們操作SQL服務器引擎,我們將使用SqlConnection對象。當我們使用SQLConnection對象創(chuàng)建連接時,我們需要使用下面的連接參數(shù):□數(shù)據(jù)源--數(shù)據(jù)源被放置位置的服務器名字。數(shù)據(jù)源可能被放在任何地方,可能是在網(wǎng)絡或互聯(lián)網(wǎng)上。通常,您使用你的本地網(wǎng)絡,你便需要存放數(shù)據(jù)源的專門計算機名字。我們也可用localhost或(local)來代替我們運行程序的計算機。這項術(shù)語,當它作為當前,本地計算機時,由許多Windows應用使用。□用戶標識和密碼--連接數(shù)據(jù)源所需要的認證,用戶標識和密碼是由數(shù)據(jù)庫管理員建立的,他防止其他用戶在未經(jīng)允許的情況下查看或更改數(shù)據(jù)庫?!醭跏蓟罩疽贿@是我們想要與之工作的數(shù)據(jù)庫。為了創(chuàng)建一個新的連接,我們聲明一個新的SqlConnection并且運用以下所示的這些參數(shù)設置ConnectionString屬性:DimmyConnectionAsNewSqlClient.SqlConnection()myConnection.ConnectionString="DataSource=localhost;"&_"InitialCatalog=NorthwindSQL;UserId=sa;Password=sa;"或者,我們可以將連接串作為一個參數(shù)傳遞給SqlConnection,如下所示:DimmyConnectionAsNewSqlClient.SqlConnection("DataSource=localhost;"&_"InitialCatalog=NorthwindSQL;UserId=sa;Password=sa;""創(chuàng)建一個新的OleDbConnection對象是簡單的,除非我們同時需要一個提供者參數(shù)來描述我們正在連接的數(shù)據(jù)源的類型。因此,為什么我們不需要那個與SqlConnection對象相關(guān)的參數(shù)?你已經(jīng)掌握它了一因為提供者類型將會一直是SQL并且,實際上如果你真的試著將提供者參數(shù)設置為SqlConnection對象,你將會出錯?,F(xiàn)在我們可以來看看這個需要一個數(shù)據(jù)連接對象來建立以便能運轉(zhuǎn)的要素,即就是DataAdapter。三、DataAdapterDataAdapter是位于數(shù)據(jù)源和數(shù)據(jù)集之間的一種機制。我們擁有兩種類型的DataAdapters:SqlDataAdapter,專門為SQLServer數(shù)據(jù)庫所使用;OleDbDataAdapter,被所有的數(shù)據(jù)源所使用并且通過另外一個被稱為OLEDB的層。因此,通過防止這個額外層的需要,SqlDataAdapter提供更快數(shù)據(jù)的訪問。OleDbDataAdapter可以被用來訪問SQLServer,但是,因為它通過OLEDB層,如果你打算就用SQLServer而不是任何其他的,為了達到最好性能你將被建議保持和SqlDataAdapter一致。這適合這章中的簡單數(shù)據(jù)庫應用程序,因此我們嚴格與SqlDataAdapter一致地工作。DataAdapter允許進行選擇、更新、刪除或者向數(shù)據(jù)源中插入數(shù)據(jù)。這些方法通過使用SelectCommand,UpdateCommand,InsertCommand,和DeleteCommand屬性來設置為了特定操作所需要的數(shù)據(jù)庫命令字符串來實現(xiàn)。這些屬性中的每一個都是一個命令對象的例子,它們的工作即就是執(zhí)行一個SQL語句或者存儲過程并且返回一個結(jié)果集。對一個SQL數(shù)據(jù)庫來說,命令對象將保持著為規(guī)定操作實際所需的選擇、更新、插入和刪除語句,比如:"SELECT*FROMProducts"或者"DELETEFROMOrdersWHERECustomerID='ABC'"。命令對象還存儲著連接信息因此它能夠連接上數(shù)據(jù)庫來執(zhí)行它所包含的SQL語句。在此指出,在我們簡化了的例子中,我們將僅用SelectCommand工作因為我們只需要從我們的數(shù)據(jù)庫中選擇信息來進行查詢。因為我們和一個SQL數(shù)據(jù)庫打交道,我們將利用SqlCommand對象(和OleDBCommand對象相對應)。當我們利用DataAdapter向?qū)r,對每個表來說你都將有一個對應的DataAdapter。當我們運用向?qū)r,為了使選中的表能夠擁有所有的對其更新和恢復信息,DataAdapters被特別配置。為了對另外一個表重用它,我們主要需要重建組成DataAdapter的對象,這意味著所有的命令對象都要被重建。簡單的解決方案是每個表分配一個DataAdapter,這樣幫助你讓你的代碼保持整潔并且容易維護。當你建立一個DataAdapter時,如果需要你可以明確指定多于一個表。比如,我們可以創(chuàng)建一個聯(lián)系顧客表和訂購表的DataAdapter一來使我們能夠只用單個DataAdapter就能從兩個表中查看信息,而不需要任何的代碼來連接它們。但是,當遇到更新信息時,這種將多個表連接成一個表視圖的方法就運行的不是很好,因為DataAdapter向?qū)Р荒軌蜻m當將表連接起來來串聯(lián)更新和刪除,強化了一個表一個DataAdapter的情況。四、數(shù)據(jù)集最后,一個數(shù)據(jù)集是表的一個容器或者集合;它能夠包含一個或者多個表并且在主存中被維護。表間關(guān)系也被保存在這里。它所保持的表將諸如顧客詳細情況或產(chǎn)品信息的信息以記錄或行的格式包含在一起。一個表可能包含幾千個這樣的行。一個有用的說明是將一個數(shù)據(jù)集想象為保持一個書籍出版者的細節(jié)。一個有技術(shù)的出版者可能以幾種類別發(fā)布書刊,比如像.NET,JAVA,ASP和C++。在每個類型中都是單獨的書刊一因此一個.NET類型應該擁有像專業(yè)版VB.NET,基本VB.NET,專業(yè)C#,和引領.NET。一張表應能代表這些種類中的一種,并且一個類型內(nèi)的每本書應該被適當?shù)谋碇械囊恍兴?。每一行保持著每本書的細?jié)一例如:標題、價格、ISBN編號、出版日期和頁數(shù)等。對于你能向數(shù)據(jù)集中存儲的信息類型并沒有限制。既然我們已經(jīng)了解了一個數(shù)據(jù)集的內(nèi)部細節(jié),就讓我們來看看怎樣在一個應用程序中應用一個數(shù)據(jù)集。對我們的程序來說,我們將需要建立一個SqlDataAdapter對象來從向?qū)?shù)據(jù)庫中通過一個SqlConnection對象選擇顧客記錄。這個連接將被僅僅維持完成SQL選擇操作的時間。我們將通過運用SqlDataAdapter對象把我們的顧客表中數(shù)據(jù)填充到我們的數(shù)據(jù)集中。連接,或者數(shù)據(jù)綁定,對諸如DataGrid控件的可視化控件將會在窗口模型中展示數(shù)據(jù)集中的內(nèi)容。現(xiàn)在我們可以在我們簡單的數(shù)據(jù)庫應用程序中執(zhí)行這些控件。下面是以下任務的一個概述:□創(chuàng)建一個窗口應用程序□與一個數(shù)據(jù)源建立連接□向我們的框架中添加一個DataAdapter□從DataAdapter產(chǎn)生一個數(shù)據(jù)集□向我們的框架中添加一個DataGrid控件□在我們的DataGrid中顯示顧客表的內(nèi)容我們以創(chuàng)建一個新的VisualStudio.NET應用程序然后再向工程中添加一個SqlDataAdapter作為開端。我們?nèi)匀恍枰獮镈ataAdapter創(chuàng)建一個與NorthwindSQL數(shù)據(jù)庫聯(lián)系的連接。從SqlDataAdapter我們將創(chuàng)建一個新的數(shù)據(jù)集。一旦我們有了數(shù)據(jù)集,我們將向我們的框架中添加一個DataGrid并且將數(shù)據(jù)集和它綁定。最后,我們將添加一個按扭來將顧客記錄填充到數(shù)據(jù)集中,并且在DataGrid中展示出來。VisualStudio.NET的配置向?qū)槲覀兲峁┝艘粋€簡單的方法來做這些。現(xiàn)在我們離創(chuàng)建我們的數(shù)據(jù)庫應用程序只有一點點距離了!3數(shù)據(jù)訪問簡史數(shù)據(jù)訪問方法自從MicrosoftAccess1.0和VisualBasic3.0時就存在了。數(shù)據(jù)庫存取工作利用VisualBasic變得很容易,好象他們?nèi)匀辉跒楹唵巫烂鎽贸绦蜃龉ぷ鳌isualBasic中的第一個數(shù)據(jù)模型被稱為數(shù)據(jù)訪問對象(ADO),在VisualBasic3.0中介紹。它能夠與MicrosoftAccess和其他類型的數(shù)據(jù)庫交互,盡管它對于你的臺式計算機來說很好用。它很容易被用因為它提供了簡單的數(shù)據(jù)訪問方法和工具,從VisualBasicIDE環(huán)境而來,比如說數(shù)據(jù)綁定控件。這些工具和大量的諸如書籍和技術(shù)報告之類的資源,意味著你能夠緊接著很快使一個數(shù)據(jù)庫應用程序運轉(zhuǎn)起來。VisualBasic環(huán)境和一個數(shù)據(jù)管理者連接在一起,這個管理者允許創(chuàng)建和管理數(shù)據(jù)庫存取,并且,當你將你的應用程序分散時,創(chuàng)建進程允許對任何需要的數(shù)據(jù)訪問圖書館進行簡單選擇。它仍然允許你寫入并運用一套代碼來訪問不同的數(shù)據(jù)源,因為,只有一件事改變,即你所想連接到的數(shù)據(jù)源的類型。這些數(shù)據(jù)源主要是非關(guān)系數(shù)據(jù)庫。非關(guān)系數(shù)據(jù)庫通常是作為連續(xù)二進制流存儲的平文件。ISAM(索引順序存取方法)數(shù)據(jù)庫運用這種類型并且提供一種控制計算機怎樣訪問存儲在硬盤上記錄和文件的途徑。數(shù)據(jù)被連續(xù)地存儲,直接訪問一個具體的記錄需要運用索引。這些文件的結(jié)構(gòu)十分復雜。很多讀取這種類型數(shù)據(jù)的引擎,在訪問記錄時非常的迅速而有效,因為不需要通過多余的層,并且他們可以直接訪問數(shù)據(jù)。這樣隱藏著一個不好的方面,即它們?nèi)狈θ魏伟踩珯C制來保護數(shù)據(jù),任何人都可以進入查看或者編輯數(shù)據(jù)庫中信息,并且也沒有歷史事務被維持來記錄是誰、什么或者什么時候記錄被改變了。這種系統(tǒng)不是為多服務框架而設計的,在多服務框架里進程可以被分開來使顧客計算機做很多的數(shù)據(jù)進程工作。它們對多用戶環(huán)境的適應力很差,并且也不提供固有的機制來阻止用戶同時進行的相互有影響的改變操作。作者:里克特.J來源:《深入理解.net》TheUserInterfacefortheDatabaseCapabledatabaseapplicationdesignersandbuildersareinvariablyconsideredagreatassettoacompanyorbusiness,andrightlyso.SuchpeopleareabletosolveanumberofproblemsinsituationswhereusersneedtointeractwithdatabeitaccessedoveraLAN,WAN,ortheInternet.Onceyouhaveacquiredtheknowledgetobuilddatabasesystems,therereallyonlyremainstheknow-howtobuildaninterfacethatallowsuserstotalktothesesystems.Thereareotheraspectsofdevelopmentyouneedtobeawareof,forinstancedesigningasystemgearedtowardsathree-tierarchitecture,andthesewillbediscussedlater.Banks,stores,andmostbusinessestodayalldependonsometypeofdatabasesystemfortheirday-to-dayoperationsand,withoutit,theywouldnolongerbeabletorunefficiently.Thisiswhereyouasthedeveloperhaveacrucialroletoplay.Tobeabletotieauserinterface,andotherrelatedbusinesscomponentsifdesired,toadatasourceisavaluableasset.Thinkofthelastapplicationyouusedaroundyouroffice,perhapsanemployeephonebook,customerdatabase,inventoryprogram,orfinanceprogram,andhowtheyallgathereddataandpresentedittoyouinafriendlymanner.Beingabletobuildthesetypesofapplication,thathavethepotentialtomakedealingwithinformationsomucheasier,canbringgreatbenefitsthroughoutacompany,andsoitisthatsuchprogrammersbecomehighlyvaluedassets.Manycompaniesnowadaysemployapersonorgroupsolelyforthemanagementofexistingdatabases-thedatabaseadministrators(DBAs)-andhaveaseparateteamofdeveloperswhosejobitistobuildtheapplicationsthemselves.Often,however,youasthedatabasedeveloperwillbeplayingbothroles,andsoyouwillneedabasicunderstandingofbothtasks.TheDBA'sresponsibilitiesinclude:KnowledgeofthestructureofthedatabaseandhowtouseandstoreinformationinanefficientmannerTheabilitytonormalizeadatabase,astalkedaboutintheChapter1Insightintothechangingdemandsofthecompanyasitgrowsandexpands-willthedatabaseinitspresentformbeabletohandletenorevenahundredtimestheamountofinformationandremainefficient?Alogicalmethodologywhenitcomestotheuserinterfacedesign,soscreensareorganizedinawaythatmakessensetotheuser-forinstancegroupingrelatedcontrolstogether,suchascustomerandaddressfields.Theabilitytoworkcloselywiththeusertodesignthesesystems.Thiswillallhelpwhenitcomestodevelopingclient-serverapplications.Whenwedevelopdatabasesweneedtoconsidersuchthingsasefficiency,flexibility,handlingmultipleusers,datalocking,datagrowth,integrity,andperformance.Thepersondevelopingtheclient-sideoftheapplicationneedstoworryabouthowtheuserinterfacewilllookaswellastheperformanceandefficiencyoftheapplication.Havingaclearideaofhowuserswillinteractwithyourdatabasesystemwillhelpindetermininghowitshouldbebuilt.Userswilloftenperformcertainproceduresinacertainorderwhenundertakingtasks,andsometaskswillbemuchmorecommonthanothers.Forinstance,ifyouaredesigningastockinventoryapplication,youmightwantyoursystemtostartupwiththescreenthatallowsuserstomakechangestotheinventory,ratherthan,say,thescreenforaddingnewproductlines.Inthischapter,welookatthefundamentalsofbuildingabasicuserinterfacetoaSQLdatasource.We'llalsolookatsomegoodpracticesthatyoushouldapplywhenbuildingmoresophisticateddatabaseapplications.Herearethetopicswewillcovertothisend:AsimpleuserinterfaceforaccessingadatabaseHowtopopulateaDataGridUsingWizardsThecodegeneratedbehindthescenesGoodpracticesforgeneraluserinterfacedesign1TheUserInterfaceSoyou'velearnedhowtodesignadatabaseandhowtoupdateinformationinitusingsuitableSQLstatements.Wecan'texpectouruserstoopenupanadministrativedatabasetoolandmanipulatethedatadirectlylikethis,soournextstepistocreatesomewayforuserstoaccessinformation.Thisistheroleoftheuserinterfacetoprovideameansofcommunicationbetweentheuserandthedatabase.Throughtheuserinterface,usersmayinteractwithdataandmanipulateitinordertoaccomplishcertaintaskssuchastoview,add,update,ordeletedetails.Theuserinterfaceanddatabasegohandinhand,andwiththerightcombination,usersareabletohandlemassiveamountsofinformationwithoutanyparticulartechnicalknowledge.Sometypeofthoughtprocessormethodologyisrequiredtocomeupwithagooddesignfortheinterface:wewanttocreateaconsistentandappropriatelooksothattheinterfaceisreadilyaccessibletothosewhoaretouseit.Weshouldbeawareofexistingbusinesslogicwhenallowingtheusertodothingslikeaddordeleteinformationtoensureit'sdonecorrectlyandtopreventusersfrominadvertentlymanipulatingdatainawaythatcouldpotentiallyresultinirrecoverablelossestothebusiness.Securityanddataintegrityareimportant,fortheyprovidethemechanismstoensureusersarenotabletoadjusttheirownbankbalances,modifypricesthemselves,orvieworevenchangeotherpeople'sinformation.Weneedawaytohidethechecksandbalancesbehindthescenes.Mostofthiscanbeaccomplishedthroughgooddesignofforms,tobuildinthecapabilitytocallupon,validate,andprotectthisinformation;eitherthroughthecodebehindtheformsorbyhavingtheformscallsanotherobject.Thereisalsothepossibilitythatamalicioususercouldbypasstheseformsentirely,andattempttodirectlyaccessthedatabase,sowatertightsecurityisvital.Toachievethis,ourformscanbemadetoaccessthedatabasewithasecretuserIDandpassword,andwecanhideourchecksandbalancesthroughstoredprocedures-thatis,proceduresstoredonthedatabaseserver.Thesestoredprocedurescanperformsomevalidationbasedonwhatparametersarepassed,andonlyreturninformationwhencorrectsecurityinformationhasbeenpassed.Allthisisaccomplishedthroughbusinessrules,whichdictatehowdatawillbemaintainedintheparticularscenariosrelevanttoourbusiness.Nowthatweknowalittleabouttheissuesinvolvedwiththeuserinterface,wecanmoveontolookathowwecanbuildaninterfaceinVisualBasic.NET.ThefundamentalcomponentofthistypeofapplicationistheWindowsForm,whichcanbeviewedasanemptycanvaswaitingtobepaintedon.Wecanplacecontrolsonsuchaformtocreatealogicallookandfeelforourinterface,andprovideanaturalandintuitivewayfortheusertocommunicatewithourdatabase.2ABriefIntroductiontoADO.NETBeforewecanbegincreatingourapplicationweneedtolearnaboutsomebasiccomponentsofADO.NETandwizardsthatourapplicationistouse.WewillgointogreaterdetailonADO.NETinthenextchapterso,fornow,we'lllearnjustenoughtogetusthroughthissimpledatabaseproject.ADO.NETprovidesuswithawayofgatheringdataandinformationandpresentingitthroughauserinterface.Byusingsomecomponents,we'reabletoconnecttovariousdatasourcesandcanthenbuildauserinterfacethataccessesadatabase.WeneedfourpiecestobuildourADO.NETproject:Adatasource-wheretheactualdataisstored,ourdatabase.AConnectionobject-forconnectingustoourdatabase.ADataAdapterobject-toprovideamechanismforreadingandwritingdatatothedatabase.ADataSetobject-thiswillcontainthetable(s)thatwewilluse.Firstly,weneedaconnectiontothedatasource,providedbyaConnectionobject.TheConnectionobjectrequirescertaininformationforittoconnecttothedatasource.TheConnectionobjectiscalledbytheDataAdapterobject,whichhandlescommandstoselect,update,insert,anddeletedatainthedatasource.FinallywehaveaDataSetthatcontainsourtablesandwhichusestheDataAdaptertopopulateitselfandtoupdateinformationinthedatasource.一、TheDataSourceAdatasourceisthetermusedtodescribeanycollectionofinformationthatcanprovidedatatous.Itcantaketheformofadatabase,anXMLdocument,aMicrosoftExcelspreadsheet,orevenaflattextorbinaryfile.Itonlytakesoneortwolinesofcodeforustochangethekindofdatasourcethatweconnectto.TheWindowsenvironmentprovidesusasharedsetofclassesforuseinourprogramstocommunicatewiththesedifferentsourcesusingsimilarcode.二、TheDataConnectionThefirstthingweneedtoconnecttoadatabaseisthedataConnectionobject.Thiscomesintwoversions-eitheraSqlConnectionorOleDbConnectionobject.AsweareworkingwiththeSQLServerDesktopEngine,wewillusetheSqlConnectionobject.WhenwecreateaconnectionusingaSqlConnectionobject,weneedtofeeditthefollowingconnectionparameters:DataSource-thenameoftheserverwhereyourdatasourceislocated.Thedatasourcecanbeanywhere,beitonyournetworkorsomewhereovertheInternet.Usually,youwillbeworkingonyourlocalnetworkandsoyouneedtospecifythenameofthecomputerthatholdsthedatasourcehere.Alternatively,wecangivethenamelocalhostor(local)tosignifythatwewanttousethecomputerthatisactuallyrunningtheapplication.ThisterminologyisusedbymanyWindowsapplicationswhenitisnecessarytoidentifythecurrent,localcomputer.UserIDandPassword-theauthenticationdetailsrequiredtocommunicatewiththedatasource.TheIDandpasswordissetupbythedatabaseadministratorandhelpspreventpeoplefromviewingormodifyingthedatabasewithoutpermission.InitialCatalog-thisisthenameofthedatabasewewanttoworkwith-inthiscase,NorthwindSQL.Tocreateanewconnection,wedeclareanewSqlConnectionandsettheConnectionStringpropertyusingtheseparametersasshownhere:DimmyConnectionAsNewSqlClient.SqlConnection()myConnection.ConnectionString="DataSource=localhost;"&_"InitialCatalog=NorthwindSQL;UserId=sa;Password=sa;"Alternatively,wecanpasstheconnectionstringasaparametertotheSqlConnectionasfollows:DimmyConnectionAsNewSqlClient.SqlConnection("DataSource=localhost;"&_"InitialCatalog=NorthwindSQL;UserId=sa;Password=sa;""CreatinganewOleDbConnectionobjectissimilar,exceptthatwealsoneedaProviderparametertodescribethetypeofdatasourcethatweareconnectingto.So本科生畢業(yè)設計(英文文獻)whydon'tweneedthatparameterwiththeSqlConnectionobject?You'vegotit-becausetheprovidertypewillalwaysbeSQLand,infact,ifyoudotrytosettheProviderparameterforanSqlConnectionobject,youwillgetanerror.NowwecanlookatthecomponentthatrequiresadataConnectionobjecttobesetupinordertofunction,namelytheDataAdapter.三、TheDataAdaptersTheDataAdapteristhemechanismthatsitsbetweenthedatasourceandtheDataSet.WehavetwotypesofDataAdapters,theSqlDataAdapter,whichisusedexclusivelyforSQLServerdatabases,andtheOleDbDataAdapter,whichisusedforallotherdatasourcesandgoesthroughanotherlayercalledOLEDB.Consequently,byavoidingtheneedforthisextralayer,theSqlDataAdapterprovidesmuchfasteraccesstodata.TheOleDbDataAdaptercanbeusedtoaccessSQLServerbut,asitthengoesthroughtheOLEDBlayer,youarewelladvisedtostickwiththeSqlDataAdapterforoptimumperformanceifyoudon'tanticipateusinganythingotherthanSQLServer.Thisappliestooursimpledatabaseapplicationinthischapter,andsoweworkstrictlywithSqlDataAdapter.TheDataAdapterallowsselecting,updating,deleting,orinsertingdatainthedatasource.ThesemethodsareaccomplishedthroughtheuseoftheSelectCommand,UpdateCommand,InsertCommand,andDeleteCommandpropertiestosettothedatabasecommandstringrequiredforthatparticularoperation.EachofthesepropertiesisaninstanceofaCommandobject,whosejobitistoexecuteaSQLstatementorstoredprocedureandreturnaresultset.ForaSQLdatabase,theCommandobjectwillholdtheactualSELECT,UPDATE,INSERT,andDELETEstatementrequiredforagivenoperation,suchas"SELECT*FROMProducts"or"DELETEFROMOrdersWHERECustomerID='ABC'".TheCommandobjectalsostoresconnectioninformationsoitmayconnecttothedatabasetoexecutetheSQLstatementthatitcontains.Notethat,inoursimplifiedcase,wewillonlybeworkingwithaSelectCommandasweonlyneedtoselectinformationfromourdatabaseforviewing.AswearedealingwithaSQLdatabase,wewillbeworkingwiththeSqlCommandobject(asopposedtotheOleDBCommandobject).WhenweusetheDataAdapterWizards,foreachtableyouworkwithyouwillhaveacorrespondingDataAdapter.WhenweusetheWizards,theDataAdaptersareconfiguredspecificallyforthechosentablesuchthatallofthemethodsforupdatingandretrievinginformationpointtothatspecifictable.Tore-usetheadapterforanothertable,wehavetoessentiallyrebuildtheobjectsthatmakeuptheDataAdapter,whichmeansalloftheCommandobjects.ThesimplersolutionistoassignoneDataAdapterpertable,andthishelpskeepyourcodeniceandcleanandeasytomaintain.WhenyoubuildaDataAdapter,youcanspecifymorethanonetableifneeded.Forexample,wecouldcreateaDataAdapterthatlinkstheCustomerstableandtheOrderstable-toenableustoviewinformationfrombothtablesusingasingleDataAdapter,withoutneedinganycodetolinkthem.Thismethodoflinkingmultipletablesintoasingleviewdoesn'tworkreallywellwhenitcomestoupdatinginformation,however,astheDataAdapterWizardisn'tabletoproperlylinktablestogethertocascadeupdatesordeletes,reinforcingthecaseforusingoneDataAdapterpertable四、TheDataSetFinally,aDataSetisacontainerorcollectionoftables;itcancontainoneormoretablesandismaintainedinmemory.Relationshipsbetweentablesarealsostoredhere.Thetablesitholdscontaininformationsuchascustomerdetailsorproductinformationintheformofrecords,orrows.Atablemayconsistofthousandsofsuchrows.OneusefulillustrationistothinkofaDataSetasholdingdetailsofabookpublisher.Atechnicalpublishermightpublishbooksinseveralcategoriessuchas.NET,Java,ASP,andC++.Withineachcategoryareindividualbooks-sothata.NETcategorycouldhavebookssuchasProfessionalVB.NET,BeginningVB.NET,ProfessionalC#,andIntroducing.NET.Atablecouldrepresenteachofthesecategories,andeachbookinacategorywouldberepresentedbyarowintheappropriatetable.Eachrowholdsdetailsforeachbook-forexample,title,price,ISBNnumber,publishingdate,andthenumberofpages.ThereisnolimittothetypeofinformationyoucanstoreinaDataSet.NowthatwehavelookedattheinternalsofaDataSet,let'stakealookathowwecanputonetouseinanapplication.Forourprogram,wewillneedtocreateaSqlDataAdapterobjecttoselectcustomerrecordsfromtheNorthwinddatabaseviaaSqlConnectionobject.ThisconnectionwillbeopenedonlylongenoughtocompletetheSQLSELECToperation.OurDataSetwillbepopulatedwithdatafromourcustomertableusingtheSqlDataAdapterobject.Linking,ordatabinding,toavisualcomponentsuchastheDataGridcontrolwillthendisplaytheDataSet'scontentsonaWindowsForm.Nowwecanbeginimplementingthesecomponentsinoursimpledatabaseapplication.Here'sanoverviewofthetasksahead:CreatingaWindowsApplicationConnectingtoadatasourceAddingaDataAdaptertoourformGeneratingaDataSetfromtheDataAdapterAddingaDataGridcontroltoourformDisplayingthecontentsofacustomertableinourDataGridWestartbycreatinganewVisualStudio.NETapplicationandthenaddingaSqlDataAdaptertotheproject.We'llalsohavetocreateaconnectiontotheNorthwindSQLdatabasefortheDataAdapter.FromtheSqlDataAdapterwe'llcreateanewDataSet.OncewehaveaDataSet,wewilladdaDataGridtoourformandbindtheDataSettoit.Lastly,we'lladdabuttonthatfillsth
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 后現(xiàn)代別墅陳設搭配課件
- 證明房產(chǎn)的合同范本
- 建設工程索賠的概念學習情境六建筑工程索賠課件
- 腦血管造影護理
- 2024-2025學年大冶市數(shù)學四年級第二學期期末經(jīng)典模擬試題含解析
- 陽光學院《服裝數(shù)字化技術(shù)CAD》2023-2024學年第二學期期末試卷
- 寧夏職業(yè)技術(shù)學院《計算機圖形圖像處理1》2023-2024學年第二學期期末試卷
- 跨領域財務成本控制與預算編制的挑戰(zhàn)與對策
- 2025廣東河源市連平縣屬國有企業(yè)職業(yè)經(jīng)理人招聘2人筆試參考題庫附帶答案詳解
- 江西農(nóng)業(yè)大學南昌商學院《藏藥礦物學》2023-2024學年第二學期期末試卷
- 第二章陸地和海洋【真題訓練】(人教版)(原卷版)
- 小吃街概念性規(guī)劃
- 創(chuàng)新小白實操手冊 第2版 課件全套 吳雋 模塊1-8 人人皆可創(chuàng)新-商業(yè)呈現(xiàn)與商業(yè)計劃
- 2024年世界職業(yè)院校技能大賽高職組“關(guān)務實務組”賽項參考試題庫(含答案)
- 電商提成合同模板
- 正念八周課件
- 服務響應時間和服務保障方案
- 蟾蜍毒抗病毒作用機制
- 光伏發(fā)電監(jiān)理合同協(xié)議
- 新能源汽車概論課件 3.1認知純電動汽車
- 【數(shù)學】小學四年級口算題大全(10000道)
評論
0/150
提交評論