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

📄 form1.vb

📁 用vb.net写的基于c/s结构的进销存程序设计源代码
💻 VB
字号:

Imports System.Data.SqlClient
Public Class Form1
    Inherits System.Windows.Forms.Form

#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 Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents cbSearchBy As System.Windows.Forms.ComboBox
    Friend WithEvents bnSearch As System.Windows.Forms.Button
    Friend WithEvents txCustomerID As System.Windows.Forms.TextBox
    Friend WithEvents txCustomerName As System.Windows.Forms.TextBox
    Friend WithEvents txContactName As System.Windows.Forms.TextBox
    Friend WithEvents TxPhone As System.Windows.Forms.TextBox
    Friend WithEvents TxFax As System.Windows.Forms.TextBox
    Friend WithEvents txparameter As System.Windows.Forms.TextBox
    <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.Label5 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.cbSearchBy = New System.Windows.Forms.ComboBox()
        Me.txparameter = New System.Windows.Forms.TextBox()
        Me.txCustomerID = New System.Windows.Forms.TextBox()
        Me.txCustomerName = New System.Windows.Forms.TextBox()
        Me.txContactName = New System.Windows.Forms.TextBox()
        Me.TxPhone = New System.Windows.Forms.TextBox()
        Me.TxFax = New System.Windows.Forms.TextBox()
        Me.bnSearch = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "查询条件"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 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(96, 24)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "客户编码"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 128)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(96, 24)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "客户名称"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 160)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(96, 24)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "联系人"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 192)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(96, 24)
        Me.Label6.TabIndex = 5
        Me.Label6.Text = "电话"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(16, 224)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(96, 24)
        Me.Label7.TabIndex = 6
        Me.Label7.Text = "传真"
        '
        'cbSearchBy
        '
        Me.cbSearchBy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbSearchBy.Items.AddRange(New Object() {"客户编码", "客户名称"})
        Me.cbSearchBy.Location = New System.Drawing.Point(112, 16)
        Me.cbSearchBy.Name = "cbSearchBy"
        Me.cbSearchBy.Size = New System.Drawing.Size(96, 20)
        Me.cbSearchBy.TabIndex = 7
        '
        'txparameter
        '
        Me.txparameter.Location = New System.Drawing.Point(112, 48)
        Me.txparameter.Name = "txparameter"
        Me.txparameter.Size = New System.Drawing.Size(136, 21)
        Me.txparameter.TabIndex = 8
        Me.txparameter.Text = "TextBox1"
        '
        'txCustomerID
        '
        Me.txCustomerID.Location = New System.Drawing.Point(112, 96)
        Me.txCustomerID.Name = "txCustomerID"
        Me.txCustomerID.Size = New System.Drawing.Size(136, 21)
        Me.txCustomerID.TabIndex = 9
        Me.txCustomerID.Text = "TextBox2"
        '
        'txCustomerName
        '
        Me.txCustomerName.Location = New System.Drawing.Point(112, 128)
        Me.txCustomerName.Name = "txCustomerName"
        Me.txCustomerName.Size = New System.Drawing.Size(136, 21)
        Me.txCustomerName.TabIndex = 10
        Me.txCustomerName.Text = "TextBox3"
        '
        'txContactName
        '
        Me.txContactName.Location = New System.Drawing.Point(112, 160)
        Me.txContactName.Name = "txContactName"
        Me.txContactName.Size = New System.Drawing.Size(136, 21)
        Me.txContactName.TabIndex = 11
        Me.txContactName.Text = "TextBox4"
        '
        'TxPhone
        '
        Me.TxPhone.Location = New System.Drawing.Point(112, 192)
        Me.TxPhone.Name = "TxPhone"
        Me.TxPhone.Size = New System.Drawing.Size(136, 21)
        Me.TxPhone.TabIndex = 12
        Me.TxPhone.Text = "TextBox5"
        '
        'TxFax
        '
        Me.TxFax.Location = New System.Drawing.Point(112, 224)
        Me.TxFax.Name = "TxFax"
        Me.TxFax.Size = New System.Drawing.Size(136, 21)
        Me.TxFax.TabIndex = 13
        Me.TxFax.Text = "TextBox6"
        '
        'bnSearch
        '
        Me.bnSearch.Location = New System.Drawing.Point(245, 12)
        Me.bnSearch.Name = "bnSearch"
        Me.bnSearch.Size = New System.Drawing.Size(56, 24)
        Me.bnSearch.TabIndex = 14
        Me.bnSearch.Text = "查询"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(320, 293)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bnSearch, Me.TxFax, Me.TxPhone, Me.txContactName, Me.txCustomerName, Me.txCustomerID, Me.txparameter, Me.cbSearchBy, Me.Label7, Me.Label6, Me.Label5, 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
        cbSearchBy.SelectedIndex = 0
        txParameter.Text = ""
        txCustomerID.Text = ""
        txCustomerName.Text = ""
        txContactName.Text = ""
        txPhone.Text = ""
        txFax.Text = ""

    End Sub

    Private Sub bnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnSearch.Click
        '定义字符串变量, 这是用来放SQL指令
        Dim sCommandText As String
        '定义Connection对象
        Dim cnNorthwind As New SqlConnection()
        '定义Command对象
        Dim cmCustomer As New SqlCommand()
        '设定Connection对象的联机字符串
        cnNorthwind.ConnectionString = _
            "Data Source = localhost;" & _
            "Initial Catalog = Northwind;" & _
            "Integrated Security = SSPI;"
        '设定Command对象的Connection属性, 也就是说要
        '透过哪个Connection对象来连接
        cmCustomer.Connection = cnNorthwind
        '设定基本的SQL指令
        sCommandText = _
            "SELECT CustomerID, CompanyName, ContactName, Phone, Fax " & _
            "FROM Customers "
        '选择依客户编号来查询
        If cbSearchBy.SelectedIndex = 0 Then
            sCommandText = sCommandText & _
                "WHERE CustomerID = @CustomerID "
            '建立cmCustomer的SqlParameter对象
            Dim prCustomerID As SqlParameter = cmCustomer.Parameters.Add( _
                New SqlParameter("@CustomerID", SqlDbType.Char, 10, _
                    "CustomerID"))
        Else
            '依客户名称来查询
            sCommandText = sCommandText & _
                "WHERE CompanyName LIKE @CompanyName" & "%"
            '建立cmCustomer的SqlParameter对象
            Dim prCompanyName As SqlParameter = cmCustomer.Parameters.Add( _
                New SqlParameter("@CompanyName", SqlDbType.Char, 40, _
                    "CompanyName"))
        End If
        '把完整的SQL指令指向cmCustomer的CommandText属性
        cmCustomer.CommandText = sCommandText
        If cbSearchBy.SelectedIndex = 0 Then
            '依客户编号来查询, 使用@CustomerID的SqlParameter
            cmCustomer.Parameters("@CustomerID").Value = txParameter.Text
        Else
            '依客户编号来查询, 使用@CompanyName的SqlParameter
            cmCustomer.Parameters("@CompanyName").Value = txParameter.Text
        End If
        '开启连接
        cnNorthwind.Open()
        '建立SqlDataReaader对象
        Dim rdCustomer As SqlDataReader
        'ExecuteReader会传回一个DataReader, 把它丢给rdCustomer
        rdCustomer = cmCustomer.ExecuteReader(CommandBehavior.CloseConnection)
        'read = True, 代表有数据
        If rdCustomer.Read Then
            txCustomerID.Text = rdCustomer.GetString(0)
            txCustomerName.Text = rdCustomer.GetString(1)
            txContactName.Text = rdCustomer.GetString(2)
            txPhone.Text = rdCustomer.GetString(3)
            txFax.Text = rdCustomer.GetString(4)
        Else
            'read = False, 表示无数据可读取
            MessageBox.Show("找不到!")
        End If
        '关闭rdCustomer, 也同时关闭cnNorthwind
        rdCustomer.Close()

    End Sub
End Class

⌨️ 快捷键说明

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