Cамоучитель по VB.NET

         

Форма результатов приложения




'frmMain.vb

Imports System.Data.SqlClient

Public Class frmMain

Inherits System.Windows.Forms.Form #Region "Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'Вызов необходим для работы дизайнера форм Windows

InitializeComponent()

' Дальнейшая инициализация выполняется

' после вызова InitializeComponent()

End Sub

' Форма переопределяет Dispose для очистки списка компонентов.

Protected Overloads Overrides



Sub Dispose(ByVal disposing As Boolean)

If Disposing Then

If Not (components Is Nothing) Then

components. Dispose()

End If

End If

MyBase.Dispose(Disposing) End Sub

Private WithEvents Label1 As System.Windows.Forms.Label

Private WithEvents Label2 As System.Windows.Forms.Label

Private WithEvents Label3 As System.Windows.Forms.Label

Private WithEvents Label4 As System.Windows.Forms.Label

Private WithEvents btnConnect As System.Windows.Forms.Button

Private WithEvents txtUID As System.Windows.Forms.TextBox

Private WithEvents txtPassword As System.Windows.Forms.TextBox

Private WithEvents txtDatabase As System.Windows.Forms.TextBox

Private WithEvents txtServer As System.Windows.Forms.TextBox

' Необходимо для работы дизайнера форм Windows

Private components As System.ComponentModel.Container

' ВНИМАНИЕ: следующий фрагмент необходим для дизайнера форм Windows

' Для его модификации следует использовать дизайнер форм.

' Не изменяйте его в редакторе!

<System.Diagnostics.DebuggerStepThrough()>

Private Sub _ Initial izeComponent()

Me.Label4 = New System.Windows.Forms.Label ()

Me.txtPassword = New System.Windows.Forms.TextBox()

Me.Label 1 = New System.Windows.Forms.Label ()

Me.txtServer = New System.Windows.Forms.TextBox()

Me.Label2 = New System.Windows.Forms.Label ()

Me.Labels = New System.Windows.Forms.Label ()

Me.txtUID - New System.Windows.Forms.TextBox()

Me.txtDatabase = New System.Windows.Forms.TextBox()

Me.btnConnect = New System.Windows.forms.Button()

Me.SuspendLayout()

'Label4

Me.Label4.Location = New System.Drawing.Point(24.176)

Me.Label 4.Name = "Label4"

Me.Label4.Size = New System.Drawing.Size(82.19)

Me.Label4.TabIndex = 0

Me.Label4.Text = "Password:"

Me.Label4.TextAlign = System.Drawi ng.ContentAlignment.MiddleRight

'txtPassword

Me.txtPassword.Location = New System.Drawing.Point(168.168)

Me ..txtPassword. Name = "txtPassword"

Me.txtPassword.PasswordChar = ChrW(42)

Me.txtPassword.Size = New System.Drawing.Size(205.22)

Me.txtPassword.Tablndex = 3

Me.txtPassword.Text = ""

'Label 1

Me.Label 1.Location = New System.Drawing.Point(24. 32)

Me.Label 1.Name = "Label1"

Me.Label 1.Size = New System.Drawing.SizeC82. 20)

Me.Label 1.Tablndex =0

Me.Label 1.Text = "Server:"

Me.Label 1.TextAli gn = System.Drawi ng.ContentAlignment.Mi ddleRight

'txtServer

Me.txtServer.Location - New System.Drawing.Point(168, 24}

Me.txtServer.Name = "txtServer"

Me.txtServer.Size = New System.Drawing.Size(205. 22)

Me.txtServer.Tablndex = 0

Me.txtServer.Text = ""

'Label 2

Me.Label2.Location = New System.Drawing.Point(24. 80)

Me.Label 2.Name = "Label 2"

Me.Label2.Size = New System.Drawing.Size(82, 20)

Me.Label2.Tablndex = 0

Me.Label 2.Text = "Database:"

Me.Label 2.TextAlign = System.Drawi ng.ContentAlignment.Mi ddleRight

'Label3

Me. Labels.Anchor = System.Windows.Forms.AnchorStyles.None

Me.Label3.Location = New System.Drawing.Point(24. 128)

Me.Labels.Name = "Label 3"

Me.Labels.Size = New System.Drawing.Size(82. 20)

Me.Labels.Tablndex = 0

Me.Labels.Text = "User ID:"

Me.Label 3.TextAli gn = System.Drawi ng.ContentAlignment.Mi ddleRi ght

'txtUID

Me.txtUID.Location = New System.Drawing.Point(168, 120)

Me.txtUID.Name = "txtUID"

Me.txtUID.Size - New System.Drawing.Size(205, 22)

Me.txtUID.Tablndex = 2

Me.txtUID.Text = ""

'txtDatabase

Me.txtDatabase.Location = New System.Drawing.Point(168. 72)

Me.txtDatabase.Name = "txtDatabase"

Me.txtDatabase.Size = New System.Drawing.Size(205. 22)

Me.txtDatabase.Tablndex = 1

Me.txtDatabase.Text = ""

'btnConnect

Me.btnConnect.Location = New System.Drawing.Point(160. 232)

Me.btnConnect.Name = "btnConnect"

Me.btnConnect.Size = New System.Drawing.Size(92, 30)

Me.btnConnect.Tablndex = 4

Me. btnConnect.Text = "SConnect"

'frmMain

Me.AutoScaleBaseSize = New System.Drawing.Size(6. 15)

Me.ClientSize - New System.Drawing.Size(408, 280)

Me.Controls.AddRange(New _

System.Wi ndows.Forms.Control(){Me.btnConnect,_

Me.txtPassword. Me.txtUID. Me.txtDatabase.

Me.txtServer.Me

.Label 4.

Me.Label3.Me

.Label 2.

Me.Label 1})

Me.Name - "frmMain" Me.Text = "DB Connector"

Me.ResumeLayout(False) End Sub

#End Region

Private Sub btnConnect_C1ick(ByVal sender As System.Object,_

ByVal e As System.EventArgs) Handles btnConnect.Click

Try

mySQLConn = New SqlConnectionC'user id=" & txtUID.Text &

";password="&txtPassword.Text & _ ";database="&txtDatabase.Text & _

";server="&txtServer.Text)

mySQLConn.Open() dbCmd.Connect!on = mySQLConn

Dim frmChild As New frmResults() frmChild.Show()

Catch except As Exception MsgBox(_

"Failed to connect for the following reason:<" & _ except.Message & ">")

End Try

End Sub

End Class

Модуль содержит следующий код:

Imports System.Data.SqlClient Module main

' Глобальные определения

Public mySQLConn As SqlConnection

Public dbReader As SqlDataReader

Public dbCmd As SqlCommand = New SqlCommand()

End Module

Модуль Modulel содержит только глобальные определения различных объектов SQL, которые должны быть доступны для обеих форм. Хотя обычно подобное использование глобальных данных в окончательных версиях программ не рекомендуется, в данном случае это позволяет сосредоточить основное внимание на выполнении операций с базой данных.





Содержание раздела