VB編程實例講解_第1頁
VB編程實例講解_第2頁
VB編程實例講解_第3頁
VB編程實例講解_第4頁
VB編程實例講解_第5頁
已閱讀5頁,還剩93頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實例53:獲得撥號網絡項目實例說明在本實例中,我們將制作一個能夠取得本地計算機上的撥號網絡的項目的應用程序。 程序運行結果如圖53-1所示。an圖53-1運行結果技術要點讀取注冊表內容 添加到列表框實現過程 新建項目打開Visual Studio.NET,選擇新建項目,在項目類型窗口中選擇“Visual Basic項目,在模板窗口中選擇Windows應用程序”,在名稱域中輸入GetDialList ”然后選擇保存 路徑。單擊“確認”。添加控件和設置屬性向窗體上添加一個 ListBox控件。將窗體的Text屬性改為 獲得撥號網絡項目”其余屬性都保持默認。 添加代碼P rivate Declare

2、 Function Reg Op enKey Lib adva pi 32.dll AliasReg Op enKeyA(ByVal hKey As Integer, ByVal l pSubKey As String, ByRef phkResult As Integer) As Integer中級篇167P rivate Declare Function RegCloseKey Lib adva pi 32.dll (ByVal hKey As Integer) As IntegerP rivate Declare Function RegEnumKey Lib adva pi32.dll

3、 Alias RegEnumKeyA(ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal l pN ame As String, ByVal cbName As Integer) As IntegerConst HKEY_CURRENT_USER As Integer = & H80000001Const ERROR_NO_MORE_ITEMS As Short = 259Const ERROR SUCCESS As Short = 0P rivate Sub Form1_Load(ByVal eventsender As System

4、.Object, ByVai eventArgs As System.EventArgs) Handles MyBase.LoadDim hKey As IntegerDim i As IntegerDim astr As VB6.FixedLengthString = New VB6.FixedLengthString(256)If Reg Op enKey(HKEY_CURRENT_USER, RemoteAccessProfile, hKey)= ERROR_SUCCESS ThenWhile RegEnumKey(hKey, i, astr.Value, 256) = ERROR_SU

5、CCESS Listl.Items.Add(astr.Value)i = i + 1End WhileRegCloseKey(hKey)End IfEnd Sub 運行程序單擊菜單“調試I啟動”或單擊圖標運行程序。小結本實例通過讀取注冊表中有關撥號網絡的項目,然后將這些項目添加到歷表框中,這 就是本程序的思路。實例54:倒計時程序實例說明本實例將制作一個倒計時程序。程序運行時,始終位于在任何窗口的最前面,雙擊窗 體結束應用程序。程序運行結果如圖54-1所示。可以在窗體上單擊鼠標右鍵,在彈出的菜單中選擇“設置倒計時”,設置界面如圖54-2所示。設定時間:01:00:55圖54-1運行結果圖54-

6、2設置倒計時界面技術要點保持窗體最前倒計時TimeSerial ()函數SetWindowPos () API 函數實現過程 新建項目,在項目類型窗口中選擇“Visual Basic項目”,“ ReverseCount”然后選擇保存打開Visual Studio.NET ,選擇“新建項目”在模板窗口中選擇“ Windows應用程序”,在名稱域中輸入 路徑。單擊“確認”。 添加控件、菜單和窗體為當前窗體添加兩個 Label控件和兩個Timer控件。其中一個Timer控件控制一個Label 控件在上方一直循環移動;另一個 Timer控件控制時間的顯示。添加一個 ContextMenu控 件并新建菜

7、單“設置倒計時”和“退出” 。利用菜單“項目 添加Windows窗體”,為當前項 目添加一個窗體 Form2,給Form2添加四個Label控件,用于說明之用;兩個 Button按鈕 和三個TextBox控件。最后通過菜單“項目I添加模塊”為當前項目添加一個模塊。 設置屬性切換到“屬性欄”,對窗體及窗體上的控件進行設置屬性。詳細情況如表54-1和表54-2所示。# 中級篇表54-1 窗體Formi及控件的屬性值窗體/控件屬性值Form1FormBorderStyleFixedSingleContextMenuContextMenu1Label1Text倒計時Timer1Interval100T

8、imer2Interval1000表54-2 窗體Form2及控件的屬性值窗體/控件屬性1值Form2BorderStyle0-NoneStartU pP ositonCenterScreenText1Text(空)Button1Text更改圖片其余控件跟界面一致即可 添加代碼Module Modulel這是在模塊中添加的代碼P ublic hours As ShortP ublic minutes As ShortP ublic seconds As ShortP ublic revtime As Date此API函數用來使某個窗口位于所有窗口之上Public Declare Functio

9、n SetWindow Pos Lib user32 (ByVai hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As IntegerEnd Module下面這些代碼是在窗體Formi中添加的雙及窗體,結束應用程序P rivate Sub Form1_DoubleClick(ByVal eventsender As System

10、.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.DoubleClickMe.Close()End Sub啟動程序時,使程序位于任何窗體的上方P rivate Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.LoadMe.To pM ost = TrueEnd Sub開始倒計時,并顯示出來P rivate Sub Timer1_Tick(ByVal eventSend

11、er As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tickrevtime = DateAdd(Microsoft.VisualBasic.Datelnterval.Second, -1, revtime)Label2.Text = Format(revtime, hh) & : & Format(revtime, mm) & : & Format(revtime, ss)End SubP rivate Sub Menultem1_Click(ByVal sender As Object, ByVal

12、 e As System.EventArgs) Handles MenuItem1.ClickForm2.Deflnstance.ShowDialog()End SubP rivate Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem2.ClickMe.Close()End Sub下面這些代碼是在窗體Form2中添加的P rivate Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventA

13、rgs As System.EventArgs) Handles Button1.Clickhours = Val(Text1.Text)minutes = Val(Text2.Text)seconds = Val(Text3.Text)將輸入轉化為時間格式revtime = TimeSerial(hours, minutes, seconds)Form1.DefInstance.Label2.Text = Format(revtime, hh) & : & Format(revtime, mm) & : & Format(revtime, ss)Form1.DefInstance.Timer

14、1.Enabled = TrueForm2.DefInstance.Hide()End SubP rivate Sub Button2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button2.ClickMe.Close()End Sub 運行程序單擊菜單“調試I啟動”或單擊圖標運行程序。小結本實例介紹了一個倒計時程序,它的思路比較簡單,就是獲得用戶輸入的時間值,通 過定時器每秒鐘更新顯示一次。它的應用比較廣泛,讀者可以更改其界面,使其變得更實 用、更漂亮。實例5

15、5:串行化數據實例說明在本實例中,我們將利用VB.NET向大家展示一個串行化數據的實例。程序運行結果如圖55-1所示。I ! kii-iri幣nibh h iliik. .Cmp44k.耳迄L葉Kfflpb fpMCapl.tn-rPrTHlPri Z圖 55-1運行結果技術要點串行化的概念進行讀寫操作實現過程 新建項目打開Visual Studio.NET,選擇“新建項目” 在模板窗口中選擇“類庫”,在名稱域中輸入 認”。,在項目類型窗口中選擇 “Visual Basic項目”, SerializeTest”,然后選擇保存路徑。單擊“確 添加代碼Imp orts SystemImp orts

16、 System.IOImp orts System.CollectionsImp orts System.Runtime.SerializationImp orts System.Runtime.Serialization.Formatters.BinaryNames pace ClassLibrary1P ublic Class SerializeTestP ublic Shared Sub Main()Console.WriteLine(Create object grap h) # 中級篇Dim l As New ArrayListDim x As IntegerFor x = 0 To

17、 9Console.WriteLine(x) l.Add(x)Next xConsole.Write(Serializing object graph to disk.)Dim s As Stream = File.O pen(foo.bin, FileMode.Create,FileAccess.ReadWrite)Dim b As BinaryFormatter = New BinaryFormatter b.Serialize(s, l) s.Close()Console.WriteLine(Co mp lete.)Console.Write(Deserializing object g

18、raph from disk.)Dim r As Stream = File.O pen(foo.bin, FileMode.O pen, FileAccess.Read)Dim c As New BinaryFormatterDim p As ArrayList = CTyp e(c.Deserialize(r), ArrayList) Console.WriteLine(Co mp lete.)Dim i As ObjectFor Each i In pConsole.WriteLine(i)Next ir.Close() Console.WriteLine(Microsoft.Visua

19、lBasic.ControlChars.CrLf & P ress Return to exit.)Console.Read()End SubEnd ClassEnd Names pace運行程序單擊菜單“調試1啟動”或單擊圖標運行程序小結o串行化是把圖式對象轉換成為線性字節串行的過程。字節序列可以發送到別處,例如 遠程計算機,然后執行序列解碼,這樣就在遠程主機內存中生成了原來圖式對象的克隆。 串行化過程在 VB.NET中是十分容易使用的, 而且是開放的,每個VB6的程序員都曾經自 覺或不自覺地使用過串行化。當任何ActiveX dll或者ActiveX exe被創建后,都有五個屬性可以設置,

20、最后一個 Persistable屬性就是VB6中的串行化。VB6中串行化的過程不需要 用戶認知,大部分開發人員也從來不管串行化做了什么和如何運作。在VB.NET中,有著公共對象庫,每個支持.NET體系的語言都可以通過System.Runtime.Serialization的名字空間來理解和使用 VB.NET的串行化特征。串行化可以替代任何流,象Memorystream, NetStream等,而不僅僅是 FileStream。實例56:控制圖形移動-匸劉-woII詢ftI.too11LCU水平滝it矗的佢實例說明在本實例中,我們將使用VScrollBar控件和HScrollBar控件來控制圖片

21、的移動,我們56-1所示。還可以設置移動的間距大小。程序運行結果如圖圖56-1運行結果技術要點控制圖片移動直接拖動圖片更改ScrollBar控件的移動間距大小實現過程 新建項目打開Visual Studio.NET,選擇新建項目”,在項目類型窗口中選擇 “Visual Basic項目”, 在模板窗口中選擇Windows應用程序”,在名稱域中輸入ScrollBarCtl ”,然后選擇保存 路徑。單擊“確認”。添加控件向當前窗體上添加一個 GroupBox控件,在GroupBox控件上放置六個 Label控件和兩 個ComboBox控件;然后再添加一個 Picture控件,一個 VScrollBa

22、r控件和一個 HScrollBar 控件和六個Label控件。按照圖56-1的運行結果排列好位置。中級篇175 設置屬性對窗體上的控件進行屬性設置。詳細情況如表56-1所示。表56-1窗體各控件的屬性值窗體/控件屬性值Pi ctureBoxP ictureHuman.j pgComboBox1ltems1,2,3,4,5ComboBox1ltmes5,10,20,50其余控件Text跟界面一致 添加代碼由于空間有限,我們僅列出主要代碼,其余代碼請看光盤。水平滾動條移動時,改變圖片的位置P rivate Sub HScrollBar1_Scroll(ByVal sender As System.

23、Object, ByVal e AsSystem.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scrolllabel11.Text = HScrollBar1.Value.ToString()hAbs Pos = CSng(HScrollBar1.Value - HScrollBar1.Minimum)p ictureBox1.Left = HScrollBar1.Right - Clnt(hScrollMulti plier * hAbs Pos)-p ictureBox1.WidthEnd Sub更改大間距移動的值P rivate

24、Sub comboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox1.SelectedlndexChangedVScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString()HScrollBar1.LargeChange = Convert.Tolnt16(comboBox1.Selectedltem.ToString() label11.Text = H

25、ScrollBar1.Value.ToString()label12.Text = VScrollBar1.Value.ToString()End Sub更改小間距移動的值P rivate Sub comboBox2_SelectedlndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox2.SelectedlndexChangedVScrollBar1.SmallChange = Convert.Tolnt16(comboBox2.Selectedltem.ToStrin

26、g()HScrollBar1.SmallChange = Convert.Tolnt16(comboBox2.Selectedltem.ToString() label11.Text = HScrollBar1.Value.ToString()label12.Text = VScrollBar1.Value.ToString()End Sub直接拖動圖片P rivate Sub p ictureBox1_MouseDown(ByVal sender As System.Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles

27、pi ctureBox1.MouseDown dragging = True(e.x, e.y)表示光標相對于圖片位置的相對坐標。我們需要將這個值保存下來oldX = e.XoldY = e.YEnd Sub 運行程序單擊菜單“調試I啟動”或單擊小結圖標運行程序。本實例通過使用ScrollBar控件來控制圖片的顯示,這在我們要顯示大圖片時是很有必 要的。通過對本實例的學習,讀者能夠掌握ScroollBar控件的基本使用方法。實例57:多彩文本實例說明57-1所示。在本實例中,我們制作一個能夠顯示多種形式文本的應用程序。程序運行后,即在窗 體上的不同區域輸出不同的文字。程序運行結果如圖魯列VBN

28、ET笹? went Acwn to 也;Jan號 hifirtnai, my baby Ihici e”Scfttchc (J cut 3A L Long wJu:;込址爲Ea iwE叫M cfd,*D 阪,Lnrliftr :丸rtliH凹皐mMl祁I: h阻 WhrrcHf she hr,She car. la jk 出as wiw waii ovse .liell neyt: rjJ m sweeUnanbkesc,iThfn r rli= wjtv inn to larajs me n flrj:lhLlflp si nRS.【waoM a b誠泌 wat ad a Smb; haPu

29、t a tELly ewJa gjld cu my W llkrk LLi=4gq ebe boTfUJcQffw tHt (lircl iruiJuit匚二aefc彳F血址 wJ 訕住 Wa乂 7餛 dftilui葉遠 遠 a Ort they 5 hardnn曾 nptiT tLp 曲踴曲山-1nptyFind tinlcHr Anri all 和ur rtrarrjE arch訓nir tc (Jr?耳aie BjuJthty ?lii:i s: ccld LdjLc 1 iLr UyUj ttvjLc : likr cm* tel mt v/hiT im aAr圖57-1運行結果技術要

30、點設定不同的Brush和Font 輸出字體實現過程 新建項目打開Visual Studio.NET,選擇新建項目”,在項目類型窗口中選擇 “Visual Basic項目”, 在模板窗口中,選擇Windows應用程序”,在名稱域中輸入GdipText ”,然后選擇保存 路徑。單擊“確認”。 添加代碼P ublic Sub New()MyBase.New()TextSa mple = Me# 中級篇InitializeCo mpo nent()serifFontFamily = New FontFamily(GenencFontFamilies.Senf) Me.SetStyle(ControlS

31、tyles.O paque. True)Me.SetStyle(ControlStyles.ResizeRedraw, True)Dim backgroundimage As Image設定背景圖片backgroundimage = NewBitma p(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(colorbars.j pg)新建一個畫刷,我們將使用它在背景圖片上畫圖backgroundBrush = New TextureBrush(backgroundlmage)設定文本圖片Dim

32、 textimage As Image = NewBitma p(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(marble.j pg) textTextureBrush = New TextureBrush(textlmage)設定要使用字體格式Me.Font = New Font(serifFontFamily, 20)titleFont = New Font(serifFontFamily, 60) textFont = New Font(serifFontFamily, 11)建立

33、一個陰影畫刷titleShadowBrush = New SolidBrush(Color.FromArgb(70, Color.Black)用設定的字體和畫刷輸出Jap anes或本Tryjap aneseFont = New Font(MS Mincho, 36) linearGradBrush = New LinearGradientBrush(New P oint(O, 0), New Point(0, 45), Color.Blue, Color.Red)Catch ex As Exce ptionMessageBox.Show(The Jap anese font MS Minch

34、o needs be p resent to run the Jap anese part of this samp le & ControlChars.CrLf & & ControlChars.CrLf & + ex.Message) doJa paneseSa mple = FalseEnd TryEnd SubProtected Overrides Sub On Paint(ByVal e As P aintEventArgs)Dim g As Grap hics = e.Gra phicsg.SmoothingMode = SmoothingMode.AntiAlias用textur

35、e畫刷填充背景,并應用到一個白畫布上g.FillRectangle(backgroundBrush, ClientRectangle)g.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White), ClientRectangle) g.DrawString(歡迎大家來到 VB.NET 的世界! , Me.Font, NewSolidBrush(Color.Black), 10, 10)Dim titleText As String =圖形示例g.DrawString(titleText, titleFont, titleShad

36、owBrush, 15, 25)g.DrawString(titleText, titleFont, textTextureBrush, 10, 20)Dim textToDraw As String =銀河文化公司Dim windowcenter As Double = Me.Dis pl ayRectangle.Width / 2Dim stringSize As SizeF = g.MeasureStnng(textToDraw, textFont)Dim startPos As Double = windowCenter - (stringSize.Width / 2) g.DrawS

37、tring(textToDraw, textFont, New SolidBrush(Color.Red), CTy pe(start Pos, Single), 10)Dim rectangle1 As RectangleF = New RectangleF(20, 150, 250, 300) g.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle1) g.DrawString(flowedText1, textFont, New SolidBrush(Color.Blue), rectangle1) 輸出居中文本Dim rec

38、tangle2 As RectangleF = New RectangleF(450, 150, 250, 300)g.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle2)Dim format As StringFormat = New StringFormat()format.Alignment = StringAlignment.Centerg.DrawString(flowedText2, textFont, New SolidBrush(Color.Blue), rectangle2, Format) 統計輸出的字符數和行

39、數Dim characters As Integer = 0Dim lines As Integer = 0g.MeasureString(flowedText2, textFont, rectangle2.Size, format, characters, lines) Dim whatRenderedText As String =共輸出了 + CType(characters. String) + 字符和+ CType(lines. String) + 行g.DrawString(whatRenderedText, textFont, New SolidBrush(Color.Black

40、), 400, 440) 旋轉剛才輸出的Japanes或本If (doJa paneseSa mple) Theng.RotateTransform(-30)g.TranslateTransform(-180, 300) g.DrawString(ja paneseText, ja paneseFont, linearGradBrush, 200, 140) g.ResetTransform()End IfEnd Sub 運行程序單擊菜單“調試I啟動”或單擊圖標運行程序。小結本實例使用不同的畫刷(Brush)和字體(Font),在不同的區域輸出不同的文字,并 可以旋轉文字,而并不需要使用API

41、函數。實例58:開發用戶自定義控件實例說明58-1所示。在本實例中將自行制作一個用戶自定義控件并在另外的一個應用程序中加以引用。程 序運行后,可以更改其中的內容,然后單擊save即可。程序運行結果如圖TP-C-ucl圖58-1運行結果技術要點定制用戶控件界面為用戶控件添加屬性和過程編譯用戶控件引用用戶控件實現過程 新建項目打開Visual Studio.NET,選擇新建項目”,在項目類型窗口中選擇“Visual Basic項目”,在模板窗口中選擇Windows控件庫”,在名稱域中輸入CustomControl”,然后選擇保存 路徑。單擊“確認”。 添加控件向當前用戶控件 UserControl

42、l上添加五個Label控件和五個TexbBox控件。 設置屬性對用戶控件 UserControl1上的控件設置屬性,如表58-1所示。# 中級篇表58-1各控件的屬性值窗體/控件屬性值UserControlBackColor& H00C00000Text5TextScrollBars2-VerticalMultiLineTrueLabel1TextIDText1Text其余 Label、TextBox 控件Text跟界面一致 添加組件類,在名稱欄中輸入通過菜單“項目I添加新項”,在彈出的對話框中選擇“組件類”Customer,單擊“確定”。 添加代碼在組件類的代碼窗口下輸入下列代碼。Imp O

43、rts SystemImp Orts System.Windows.FormsImp orts System.DrawingNames pace Microsoft.Sa mpl es.WinForms.VB.CustomerControlP ublic Class CustomerControlInherits System.Windows.Forms.UserControlP rivate customer1 As CustomerP ublic Sub New()MyBase.New() Required by the Windows Forms DesignerInitializeCo

44、 mpo nent() TODO: Add any constructor code after InitializeCo mpo nent call End SubP ublic Prop erty Customer() As CustomerGetReturn customer1End GetSet(ByVal Value As Customer)customer1 = ValueLoadCustomer()End SetEnd Prop ertyP ublic Sub Acce ptChanges()customer1.Title = textBoxTitle.Text中級篇181cus

45、tomer1.FirstName = textBoxFirstName.Text customer1.LastName = textBoxLastName.Text customer1.Address = textBoxAddress.Text End SubP ublic Sub RejectChanges()LoadCustomer()End SubPrivate Sub LoadCustomer()textBoxID.Text = customer1.IDtextBoxTitle.Text = customer1.Title textBoxFirstName.Text = custome

46、r1.FirstName textBoxLastName.Text = customer1.LastName textBoxAddress.Text = customer1.Address End SubP ublic Overloads Overrides Sub Dispo se()MyBase.Dis pose()End SubEnd ClassEnd Names pace右鍵單擊“解決方案” 項目。并在窗體上添加一個測試用戶控件 ,選擇“添加I新建項目”,為解決方案添加一個測試用戶控件的CustomerControl 控件和兩個 Button 控件。 添加測試代碼這些必不可少Imp O

47、rts SystemImp orts System.Co mpo nentModelImp orts System.DrawingImp orts System.Windows.FormsImp orts Microsoft.Sa mpl es.WinForms.VB.CustomerControlImp orts Microsoft.VisualBasic.ControlCharsNames pace Microsoft.Sa mpl es.WinForms.VB.HostA ppP ublic Class HostA ppInherits System.Windows.Forms.Form

48、P ublic Sub New()MyBase.New()HostA pp = MeThis call is required by the Windows Forms Designer.InitializeCo mpo nent()TODO: Add any initialization after the InitializeCo mpo nent() call CustomerControl1.Customer = Customer.ReadCustomer()Set the minimum form size to the client size + the height of the

49、 title bar Me.MinimumSize = New Size(400, (373 + SystemInformation.TextHeight)# 中級篇End SubForm overrides dis pose to clean up the compo nent list.P ublic Overloads Overrides Sub Dispo se()MyBase.Dis pose()compo nents.Dis pose()End SubP rivate Sub buttonCancel_Click(ByVal sender As Object, ByVal e As

50、System.EventArgs) Handles buttonCancel.Click CustomerControl1.RejectChanges()End SubP rivate Sub buttonSave_Click(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles buttonSave.Click CustomerControl1.Acce ptChanges() MessageBox.Show(Customer Changes Saved: & CrLf & CustomerControl1.Customer.

51、ToString)End Sub Shared Sub Main()AppI ication.Run(New HostA pp()End SubEnd Class HostA ppEnd Names pace 運行程序單擊菜單“調試I啟動”或單擊圖標運行程序。小結我們在本實例中自制了一個AcitveX控件,此控件具有定時提示的作用。讀者可以自行開發控件,最后將該控件發布即可。實例59:連接數據庫(一)實例說明其運行結果如圖59-1F面,我們將利用VB.NET自行創建一個連接數據庫的應用程序,所示。i件:u貶&fcF細 Xfttu 働(日4- -.r卅”1 tc亍入欄進呼 MJ曠砂 d 詰* m

52、旦d龜Mff *KE UttoilLrM.FKT7T1ApA TruJlUt 丹略yjurruaAntorila Harerio TaqiicraAbtautArourk tht ktdmIf 耳1141 fBIAirSBJrOorr Sc e on.BLCFP31(XJt40dA41Vl 110KHLIDCundriti【,訥MpnmFnan叫時B 口 F7HBottcnn-DfiJ_1A.T Ha工HetsU s- bcTerqEXCJIX JUCuUbH丄丄W 曲fwntto cuurcm HoctiunuQWPSCh(x&-ffliey ChineeJjCiE 1C!rtTnArrt

53、A 補piei圖59-1運行結果技術要點介紹 ADO.NETSqlConnection 禾R SqlCommand實現過程 新建項目打開Visual Studio .NET7.0,選擇 新建項目”,在項目類型窗口中,選擇 “VisualBasic 項目”,在模板窗口中,選擇ASP .NET Web應用程序”,在名稱域中輸入adooverview3 ”,然后選擇保存路徑。單擊確認。 添加代碼Imp Orts SystemImp Orts System.DataImp orts System.Data.SqlClientnames pace HowTo.Sa mpl es.ADONETp ubli

54、c class adooverview3p ublic shared sub Main()Dim myadooverview3 as adooverview3 myadooverview3 = new adooverview3() # 中級篇myadooverview3.Run()end subp ublic sub Run()Dim myReader as SqlDataReaderDim mySqlConnection as SqlConnectionDim mySqlCommand as SqlCommand設置數據庫的連接mySqlConnection = new SqlConnection(server=(local)NetSDK;uid=QSUser;p wd=QS Password;database=northwind)mySqlCommand = new SqlCommand(select * from customers, mySqlConnection) try輸出字段名稱mySqlConnection .Op en()myReader = mySqlCommand.ExecuteReader()Console.Write(Cu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論