如何从类中调用模块?

时间:2016-03-19 18:22:47

标签: mysql vb.net module

你知道,我们的教师会给我们一些代码来研究,我打算用它们来创建一个登录系统,但我不知道如何。我对此很新,我很想学习。我非常需要帮助。

Imports MySql.Data.MySqlClient

Module MySqlManager

    Public Function GetMySqlConnStr() As String
        Dim connStrBuilder As New MySqlConnectionStringBuilder

        With connStrBuilder
            .Server = "localhost"
            .UserID = "root"
            .Password = ""
            .Database = "bloodbank"
            .Port = 3306
        End With

        ''Returns the mysql connection string
        Return connStrBuilder.ConnectionString
    End Function


    ''' <summary>
    ''' Use ExecuteNonQuery for INSERT/UPDATE/DELETE query. Returns 0 if failed.
    ''' </summary>
    ''' <param name="commandText"></param>
    ''' <param name="parameters"></param>
    Public Function ExecuteNonQuery(ByVal commandText As String, ByVal ParamArray parameters As MySqlParameter()) As Integer
        Using conn As New MySqlConnection(GetMySqlConnStr)
            Using cmd As New MySqlCommand(commandText, conn)
                conn.Open()

                cmd.Prepare()
                cmd.CommandType = CommandType.Text
                If parameters IsNot Nothing Then
                    cmd.Parameters.AddRange(parameters)
                End If

                Return cmd.ExecuteNonQuery()

            End Using
        End Using
    End Function

    ''' <summary>
    ''' Use ExecuteScalar for querying a single value
    ''' </summary>
    ''' <param name="commandText"></param>
    ''' <param name="parameters"></param>
    Public Function ExecuteScalar(commandText As String, ParamArray parameters As MySqlParameter()) As Object
        Using conn As New MySqlConnection(GetMySqlConnStr)
            Using cmd As New MySqlCommand(commandText, conn)
                conn.Open()

                cmd.Prepare()
                cmd.CommandType = CommandType.Text
                If parameters IsNot Nothing Then
                    cmd.Parameters.AddRange(parameters)
                End If

                Return cmd.ExecuteScalar()
            End Using
        End Using
    End Function

    ''' <summary>
    ''' Use ExecuteReader when you are making an SQL call that will return a record set from a table.
    ''' </summary>
    ''' <param name="commandText"></param>
    ''' <param name="parameters"></param>
    Public Function ExecuteReader(ByVal commandText As String, ByVal ParamArray parameters As MySqlParameter()) As MySqlDataReader
        Dim conn As New MySqlConnection(GetMySqlConnStr)
        Using cmd As New MySqlCommand(commandText, conn)

            conn.Open()

            cmd.Prepare()
            cmd.CommandType = CommandType.Text

            If parameters IsNot Nothing Then
                cmd.Parameters.AddRange(parameters)
            End If

            Dim reader As MySqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

            Return reader

        End Using

    End Function
End Module

1 个答案:

答案 0 :(得分:1)

Class SomClass
Dim stringValue as string = MySqlManager.GetMySqlConnStr()
End Class

moduleclass之间的区别在于,当您可以拥有多个instance时,module只能有一个instances of class