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

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
    Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
                        Friend WithEvents DACustomers As System.Data.SqlClient.SqlDataAdapter
    Friend WithEvents SqlSelectCommand2 As System.Data.SqlClient.SqlCommand
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Customers1 As StoredProc.Customers
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtOrders As System.Windows.Forms.TextBox
    Friend WithEvents txtAmount As System.Windows.Forms.TextBox

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DACustomers = New System.Data.SqlClient.SqlDataAdapter()
        Me.SqlSelectCommand2 = New System.Data.SqlClient.SqlCommand()
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
        Me.txtAmount = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.Customers1 = New StoredProc.Customers()
        Me.txtOrders = New System.Windows.Forms.TextBox()
        CType(Me.Customers1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DACustomers
        '
        Me.DACustomers.SelectCommand = Me.SqlSelectCommand2
        Me.DACustomers.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Customers", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"), New System.Data.Common.DataColumnMapping("CompanyName", "CompanyName")})})
        '
        'SqlSelectCommand2
        '
        Me.SqlSelectCommand2.CommandText = "SELECT CustomerID, CompanyName FROM dbo.Customers"
        Me.SqlSelectCommand2.Connection = Me.SqlConnection1
        '
        'SqlConnection1
        '
        Me.SqlConnection1.ConnectionString = "data source=(local);initial catalog=Northwind;persist security info=False;user id" & _
        "=sa;workstation id=PROTSERVER;packet size=4096"
        '
        'txtAmount
        '
        Me.txtAmount.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtAmount.Location = New System.Drawing.Point(336, 64)
        Me.txtAmount.Name = "txtAmount"
        Me.txtAmount.TabIndex = 5
        Me.txtAmount.Text = ""
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(270, 34)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 23)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Orders"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(270, 66)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 23)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Amount"
        '
        'SqlCommand1
        '
        Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlCommand1.Connection = Me.SqlConnection1
        '
        'ListBox1
        '
        Me.ListBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedItem", Me.Customers1, "Customers.CompanyName"))
        Me.ListBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Customers1, "Customers.CustomerID"))
        Me.ListBox1.DataSource = Me.Customers1.Customers
        Me.ListBox1.DisplayMember = "CompanyName"
        Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ListBox1.ItemHeight = 14
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(256, 200)
        Me.ListBox1.TabIndex = 1
        Me.ListBox1.ValueMember = "CustomerID"
        '
        'Customers1
        '
        Me.Customers1.DataSetName = "Customers"
        Me.Customers1.Locale = New System.Globalization.CultureInfo("en-US")
        Me.Customers1.Namespace = "http://www.tempuri.org/Customers.xsd"
        '
        'txtOrders
        '
        Me.txtOrders.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtOrders.Location = New System.Drawing.Point(336, 32)
        Me.txtOrders.Name = "txtOrders"
        Me.txtOrders.Size = New System.Drawing.Size(40, 23)
        Me.txtOrders.TabIndex = 4
        Me.txtOrders.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 213)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtAmount, Me.txtOrders, Me.Label2, Me.Label1, Me.ListBox1})
        Me.Name = "Form1"
        Me.Text = "Stored Procedure Demo"
        CType(Me.Customers1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DACustomers.Fill(Customers1)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        SqlConnection1.Open()
        SqlCommand1.CommandType = CommandType.StoredProcedure
        SqlCommand1.CommandText = "OrdersPerCustomer"
        If SqlCommand1.Parameters.Count = 0 Then
            SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CustomerID", SqlDbType.NChar))
            SqlCommand1.Parameters(0).Direction = ParameterDirection.Input
        End If
        SqlCommand1.Parameters(0).Value = ListBox1.SelectedValue.ToString
        Dim SQLReader As System.Data.sqlclient.SqlDataReader
        SQLReader = SqlCommand1.ExecuteReader
        txtOrders.Text = ""
        txtAmount.Text = ""
        While SQLReader.Read
            txtOrders.Text = SQLReader.Item("Total Orders").ToString
            txtAmount.Text = Format(SQLReader.Item("Total Amount"), "#,###.00")
        End While
        SqlConnection1.Close()
    End Sub
End Class

⌨️ 快捷键说明

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