⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 用vb.net写的基于c/s结构的进销存程序设计源代码
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim WithEvents cnDatabaseconnect As New SqlClient.SqlConnection()
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents txServerName As System.Windows.Forms.TextBox
    Friend WithEvents txDatabaseName As System.Windows.Forms.TextBox
    Friend WithEvents txUserName As System.Windows.Forms.TextBox
    Friend WithEvents txPassword As System.Windows.Forms.TextBox
    Friend WithEvents bnOpen As System.Windows.Forms.Button
    Friend WithEvents bnClose As System.Windows.Forms.Button
    Friend WithEvents ckSSPI As System.Windows.Forms.CheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.txServerName = New System.Windows.Forms.TextBox()
        Me.txDatabaseName = New System.Windows.Forms.TextBox()
        Me.txUserName = New System.Windows.Forms.TextBox()
        Me.txPassword = New System.Windows.Forms.TextBox()
        Me.ckSSPI = New System.Windows.Forms.CheckBox()
        Me.bnOpen = New System.Windows.Forms.Button()
        Me.bnClose = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 14)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(88, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "主机名称"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 51)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 24)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "数据库名称"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 96)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(88, 24)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "使用者代号"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 136)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 24)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "密码"
        '
        'txServerName
        '
        Me.txServerName.Location = New System.Drawing.Point(128, 16)
        Me.txServerName.Name = "txServerName"
        Me.txServerName.Size = New System.Drawing.Size(104, 21)
        Me.txServerName.TabIndex = 4
        Me.txServerName.Text = ""
        '
        'txDatabaseName
        '
        Me.txDatabaseName.Location = New System.Drawing.Point(128, 53)
        Me.txDatabaseName.Name = "txDatabaseName"
        Me.txDatabaseName.Size = New System.Drawing.Size(104, 21)
        Me.txDatabaseName.TabIndex = 5
        Me.txDatabaseName.Text = ""
        '
        'txUserName
        '
        Me.txUserName.Location = New System.Drawing.Point(128, 98)
        Me.txUserName.Name = "txUserName"
        Me.txUserName.Size = New System.Drawing.Size(104, 21)
        Me.txUserName.TabIndex = 6
        Me.txUserName.Text = ""
        '
        'txPassword
        '
        Me.txPassword.Location = New System.Drawing.Point(128, 138)
        Me.txPassword.Name = "txPassword"
        Me.txPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
        Me.txPassword.Size = New System.Drawing.Size(104, 21)
        Me.txPassword.TabIndex = 7
        Me.txPassword.Text = ""
        '
        'ckSSPI
        '
        Me.ckSSPI.Location = New System.Drawing.Point(16, 176)
        Me.ckSSPI.Name = "ckSSPI"
        Me.ckSSPI.Size = New System.Drawing.Size(216, 24)
        Me.ckSSPI.TabIndex = 8
        Me.ckSSPI.Text = "使用WindowsNT完整性"
        '
        'bnOpen
        '
        Me.bnOpen.Location = New System.Drawing.Point(272, 16)
        Me.bnOpen.Name = "bnOpen"
        Me.bnOpen.Size = New System.Drawing.Size(72, 24)
        Me.bnOpen.TabIndex = 9
        Me.bnOpen.Text = "连线"
        '
        'bnClose
        '
        Me.bnClose.Location = New System.Drawing.Point(272, 64)
        Me.bnClose.Name = "bnClose"
        Me.bnClose.Size = New System.Drawing.Size(72, 24)
        Me.bnClose.TabIndex = 10
        Me.bnClose.Text = "中断"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(376, 205)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bnClose, Me.bnOpen, Me.ckSSPI, Me.txPassword, Me.txUserName, Me.txDatabaseName, Me.txServerName, Me.Label4, Me.Label3, Me.Label2, Me.Label1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bnClose.Enabled = False
    End Sub

    Private Sub bnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnClose.Click
        cnDatabaseconnect.Close()
        cnDatabaseconnect.Dispose()
        cnDatabaseconnect.ConnectionString = Nothing
    End Sub

    Private Sub ckSSPI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckSSPI.Click
        txUserName.Enabled = (Not ckSSPI.Checked)
        txPassword.Enabled = (Not ckSSPI.Checked)
    End Sub

    Private Sub cnDatabaseconnect_StateChange(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles cnDatabaseconnect.StateChange
        bnOpen.Enabled = (cnDatabaseconnect.State = ConnectionState.Closed)
        bnClose.Enabled = (cnDatabaseconnect.State = ConnectionState.Open)
        txDatabaseName.ReadOnly = (cnDatabaseconnect.State = ConnectionState.Open)
        txServerName.ReadOnly = (cnDatabaseconnect.State = ConnectionState.Open)
        txUserName.ReadOnly = (cnDatabaseconnect.State = ConnectionState.Open)
        txPassword.ReadOnly = (cnDatabaseconnect.State = ConnectionState.Open)
        ckSSPI.Enabled = (cnDatabaseconnect.State = ConnectionState.Closed)
    End Sub

    Private Sub bnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnOpen.Click
        Try
            cnDatabaseconnect.ConnectionString = _
                "Data Source=" & txServerName.Text & ";" & _
                "Initial catalog=" & txDatabaseName.Text & ";"
            If ckSSPI.Checked Then
                cnDatabaseconnect.ConnectionString = _
                cnDatabaseconnect.ConnectionString & _
                "Integrated Security=SSPI;"
            Else
                cnDatabaseconnect.ConnectionString = _
                    cnDatabaseconnect.ConnectionString & _
                    "Integrated Security=False;" & _
                    "User ID=" & txUserName.Text & ";" & _
                    "Password=" & txPassword.Text & ";"
            End If
            cnDatabaseconnect.Open()
        Catch eErr As System.Data.SqlClient.SqlException
            Dim sErrMsg As String
            Dim se As System.Data.SqlClient.SqlError
            For Each se In eErr.errors
                Select Case se.Number
                    Case 17
                        sErrMsg = "服务器名称错误"
                    Case 4060
                        sErrMsg = "找不到数据库"
                    Case 18456
                        sErrMsg = "错误的使用者名称或密码"
                    Case Else
                        sErrMsg = se.Message
                End Select
                MessageBox.Show(sErrMsg, "数据库联线错误" & _
                se.Number, MessageBoxButtons.OK, _
                MessageBoxIcon.Error)
            Next
        Catch eErr As System.Exception
            MessageBox.Show(eerr.Message, "不可预期的错误", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            txUserName.Text = ""
            txPassword.Text = ""
        End Try
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -