vbscript 数据库返回参数

DatabaseReturnParameter
 Public Function InsertLog(_machineName As String) As Integer
        Dim fmt As New dbFunctions
        Dim newID As Integer = -1

        Using con As New MySqlConnection(MyConnectionString)
            Using cmd As New MySqlCommand("UpdateProgressLog_Insert", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@_startDateTime", DateTime.Now)
                cmd.Parameters("@_startDateTime").Direction = ParameterDirection.Input
                cmd.Parameters.AddWithValue("@_endDateTime", Nothing)
                cmd.Parameters("@_endDateTime").Direction = ParameterDirection.Input
                cmd.Parameters.AddWithValue("@_machinename", _machineName)
                cmd.Parameters("@_machinename").Direction = ParameterDirection.Input
                cmd.Parameters.AddWithValue("@_lastID", newID)
                cmd.Parameters("@_lastID").Direction = ParameterDirection.Output

                con.Open()

                cmd.ExecuteNonQuery()

                If IsDBNull(cmd.Parameters("@_lastID").Value) Then
                    newID = -1
                Else
                    newID = fmt.ConvertdbToInteger(cmd.Parameters("@_lastID").Value)
                End If

            End Using
        End Using

        Return newID
    End Function


'PROCEDURE intechdbtest.UpdateProgressLog_Insert(IN _startDateTime datetime, IN _endDateTime datetime, IN _machineName varchar(20), OUT _lastID int)
'BEGIN
'
'  INSERT INTO updateprogresslog (StartDateTime
'  , EndDateTime
'  , MachineName)
'    VALUES (_startDateTime, _endDateTime, _machineName);
'
'  SET _lastID = LAST_INSERT_ID();
'END

vbscript 数据库删除

DatabaseDelete
Public Sub Delete(_id As Integer)
    Using con As New MySqlConnection(MyConnectionString)
        Using cmd As New MySqlCommand("Users_DeleteByID", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@_id", _id)
            con.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

vbscript 数据库插入

DatabaseInsert
Public Sub Insert(_dto As UserDTO)
    Using con As New MySqlConnection(MyConnectionString)
        Using cmd As New MySqlCommand("User_Insert", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@_usertype", _dto.UserType)
            cmd.Parameters.AddWithValue("@_firstName", _dto.FirstName)
            cmd.Parameters.AddWithValue("@_lastName", _dto.LastName)
            con.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

vbscript 数据库返回表

DatabaseReturnTable
Public Function GetAllTable() As DataTable
    Dim db As New dbFunctions
    Dim table As New DataTable
    Dim myAdapter As New MySqlDataAdapter

    Using con As New MySqlConnection(MyConnectionString)
        Using cmd As New MySqlCommand("Users_GetAll", con)
            cmd.CommandType = CommandType.StoredProcedure

            myAdapter.SelectCommand = cmd
            myAdapter.Fill(table)
        End Using
    End Using

    Return table
End Function

vbscript 数据库返回列表

DatabaseReturnList
Public Function GetProductsByCategoryID(_categoryID As Integer) As List(Of ProductDTO)
    Dim fmt As New dbFunctions
    Dim dto As New ProductDTO
    Dim list As New List(Of ProductDTO)

    Using con As New MySqlConnection(MyConnectionString)
        Using cmd As New MySqlCommand("Product_GetProductsByCategoryID", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@_categoryID", _categoryID)
            con.Open()

            Using reader As MySqlDataReader = cmd.ExecuteReader
                While reader.Read()
                    dto = New ProductDTO
                    dto.ID = fmt.ConvertdbToInteger(reader("id"))
                    dto.Name = reader("name").ToString
                    dto.WholesalePrice = fmt.ConvertdbToDecimal(reader("WholesalePrice"))
                    dto.Cost = fmt.ConvertdbToDecimal(reader("Cost"))
                    list.Add(dto)
                End While
            End Using

        End Using
    End Using

    Return list
End Function

vbscript 具有键和值的组合框

ComboBoxwithKeyandValue
Use a Dictionary to load the combobox

    Dim table As New DataTable
    table = dbEstimateStatuses.GetAllActive()
    Dim comboSource As New Dictionary(Of Integer, String)()
    For Each row As DataRow In table.Rows
        comboSource.Add(row("id"), row("description"))
    Next

    cmbStatus.DataSource = New BindingSource(comboSource, Nothing)
    cmbStatus.DisplayMember = "Value"
    cmbStatus.ValueMember = "Key"

To retrive a value from the selected item, cast the selected item to a KeyValuePair

    Dim id As Integer = -1
    Dim description As String = ""

    If Not cmbStatus.SelectedItem Is Nothing Then
        id = DirectCast(cmbStatus.SelectedItem, KeyValuePair(Of Integer, String)).Key
        description = DirectCast(cmbStatus.SelectedItem, KeyValuePair(Of Integer, String)).Value
    End If

To set a selected item

    cmbStatus.SelectedValue = 2

or

    cmbStatus.SelectedIndex = cmbStatus.FindStringExact("Pending")

vbscript 使用MySql TinyInt返回表

ReturntablewithMySqlTinyInt
In this example the MasterTable_GetOne stored procedure returns a result set with a MySql TinyInt(1) column named Active that can be converted to a Boolean in .Net. Just load the schema before filling the table and set the datatype of the column.


    
    Public Shared Function GetOne(_id as Integer) As DataTable
        Dim table As New DataTable
        Dim myAdapter As New MySqlDataAdapter

        Using con As New MySqlConnection(MyConnectionString)
            Using cmd As New MySqlCommand("MasterTable_GetOne", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@_id", _id)
                myAdapter.SelectCommand = cmd

                myAdapter.FillSchema(table, SchemaType.Source)
                table.Columns("active").DataType = GetType(Boolean)
                table.Columns("active").AllowDBNull = False
                myAdapter.Fill(table)

            End Using
        End Using

        Return table
    End Function

vbscript 数据库执行标量

DatabaseExecuteScalar
Public Shared Function GetCountByStatusID(_statusID As Integer) As Integer
    Dim count As Integer = 0

    Using con As New MySqlConnection(DHO.ConnectionString_Main)
        Using cmd As New MySqlCommand("Master_CountByStatusID", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@_statusID", _statusID)
            con.Open()
            count = Convert.ToInt32(cmd.ExecuteScalar())
        End Using
    End Using

    Return count
End Function

vbscript 事件示例2

事件使用Custom EventArgs类的示例

EventsExample2
' Class that fires the event

Public Class ucProductBOM
    ....
    Public Event BOMCostChanged(ByVal sender As Object, ByVal e As BomCostEventArgs)
    ....
    
    Private Sub SomeRoutine()
        ....
        Dim args As New BomCostEventArgs(mBOMCost)
        RaiseEvent BOMCostChanged(Me, args)
        ....    
    End Sub

End Class

' Event Args Class
Public Class BomCostEventArgs
        Inherits System.EventArgs

    Public BOMCost As Decimal

    Public Sub New(ByVal _bomCost As Decimal)
        MyBase.New
        BOMCost = _bomCost
    End Sub
End Class
    
    
' Class that handles the event    

....
Private Sub BomCostChangedHandler(sender As Object, e As ucProductBOM.BomCostEventArgs) Handles UcProductBOM1.BOMCostChanged
    Me.UcProductDetail1.UpdateBOMCost(e.BOMCost)
End Sub    
....

vbscript TimeSpanHelper

TimeSpanHelper
Public Class TimeSpanHelper

    Dim caller_name As String
    Dim timer_start As DateTime
    Dim time_spent As System.TimeSpan
    Dim prev_time As DateTime
    Dim time_diff As System.TimeSpan

    Public Sub New(caller As String)
        caller_name = caller
    End Sub

    Public Sub startClock()
        timer_start = DateTime.Now
        Console.WriteLine(caller_name & " starts at:" & timer_start.ToString)
        prev_time = timer_start
    End Sub

    Public Sub getElapsedTime(Optional flag As String = "")
        time_spent = Now.Subtract(timer_start)
        time_diff = Now.Subtract(prev_time)
        prev_time = DateTime.Now
        Console.WriteLine(caller_name & "-" & flag & " " & time_spent.TotalSeconds & "s from start (" & time_diff.TotalSeconds & "s from previous flag)")
    End Sub

    Public Sub stopClock()
        Dim timer_stop As DateTime = DateTime.Now
        time_spent = Now.Subtract(timer_start)
        time_diff = Now.Subtract(prev_time)
        Console.WriteLine(caller_name & " ends at:" & timer_stop.ToString & " - " & time_spent.TotalSeconds & " seconds elapsed from start (" & time_diff.TotalSeconds & "s from previous flag)")
    End Sub

End Class