gpt4 book ai didi

mysql - 如何从类中调用模块?

转载 作者:行者123 更新时间:2023-11-29 11:39:03 26 4
gpt4 key购买 nike

你看,我们的老师给了我们一些代码来学习,我计划使用它们来创建一个登录系统,但我不知道如何做。我对此相当陌生,并且渴望学习。我非常需要帮助。

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

最佳答案

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

moduleclass 之间的区别在于,当您可以时,您只能拥有 module 的一个实例。有多个类实例

关于mysql - 如何从类中调用模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com