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

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Option Strict On
Imports System.Data.SqlClient
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 SqlConnection1 As System.Data.SqlClient.SqlConnection
    Friend WithEvents my As System.Windows.Forms.Button

    '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.my = New System.Windows.Forms.Button()
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
        Me.SuspendLayout()
        '
        'my
        '
        Me.my.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.my.Location = New System.Drawing.Point(6, 39)
        Me.my.Name = "my"
        Me.my.Size = New System.Drawing.Size(206, 38)
        Me.my.TabIndex = 0
        Me.my.Text = "Perform Transaction"
        '
        'SqlConnection1
        '
        Me.SqlConnection1.ConnectionString = "data source=(local);initial catalog=Northwind;persist security info=False;user id" & _
        "=sa;workstation id=PROTSERVER;packet size=4096"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(226, 127)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.my})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' To test this project you must first attach the NewOrder and NewOrderLine 
    ' stored procedures to the database. Read the README.TXT file, or Chapter 22 of 
    ' Mastering VB. NET for more information
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles my.Click
        Dim myConnection As New SqlConnection()
        myConnection.ConnectionString = "data source=(local);initial catalog=Northwind;integrated security=SSPI"
        myConnection.Open()

        Dim myCommand As New SqlCommand()
        myCommand.Connection = myConnection

        Dim myTrans As SqlTransaction
        myTrans = myConnection.BeginTransaction()
        myCommand.Transaction = myTrans

        Try
            myCommand.CommandText = "NewOrder"
            myCommand.CommandType = CommandType.StoredProcedure
            Dim p As New SqlParameter()
            p.ParameterName = "@CustID"
            p.Direction = ParameterDirection.Input
            p.SqlDbType = SqlDbType.Char
            p.Value = "BLAUS"
            myCommand.Parameters.Add(p)
            p = New SqlParameter()
            p.ParameterName = "RETURN"
            p.Direction = ParameterDirection.ReturnValue
            p.SqlDbType = SqlDbType.Int
            myCommand.Parameters.Add(p)

            myCommand.ExecuteScalar()
            Dim orderID As Integer = CType(myCommand.Parameters("RETURN").Value, Integer)

            myCommand.CommandText = "NewOrderLine"
            myCommand.CommandType = CommandType.StoredProcedure
            myCommand.Parameters.Clear()
            p = New SqlParameter()
            p.ParameterName = "@OrderID"
            p.Direction = ParameterDirection.Input
            p.SqlDbType = SqlDbType.Int
            p.Value = orderID
            myCommand.Parameters.Add(p)

            p = New SqlParameter()
            p.ParameterName = "@ProductID"
            p.Direction = ParameterDirection.Input
            p.SqlDbType = SqlDbType.Int
            p.Value = 15
            myCommand.Parameters.Add(p)

            p = New SqlParameter()
            p.ParameterName = "@Quantity"
            p.Direction = ParameterDirection.Input
            p.SqlDbType = SqlDbType.Int
            p.Value = 1
            myCommand.Parameters.Add(p)
            myCommand.ExecuteNonQuery()


            p = myCommand.Parameters("@ProductID")
            p.Value = 25
            p = myCommand.Parameters("@Quantity")
            p.Value = 2
            myCommand.ExecuteNonQuery()

            p = myCommand.Parameters("@ProductID")
            p.Value = 35
            p = myCommand.Parameters("@Quantity")
            p.Value = 3
            myCommand.ExecuteNonQuery()

            myTrans.Commit()
            Console.WriteLine("New order written to database.")
        Catch exc As Exception
            myTrans.Rollback()
            Console.WriteLine(exc.Message)
            Console.WriteLine("Invalid data, database was not updated.")
        Finally
            myConnection.Close()
        End Try
    End Sub
End Class

⌨️ 快捷键说明

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