Skip to main content
added some review about the properties in the class
Source Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188

yourYou created public properties for

  • UserName
  • Password
  • Server
  • Database

but you shouldn't allow these things to be editable by other objects

these are things that need to be hidden from everyone except this class, the consumer of this class should not know what these values are, or be allowed to change them without following a strict set of rules defined in the class itself

I think that what you want is a protected property, something that is only available to the class and any class that inherits the class.

the Server property would look like this:

Protected Property Server
    Get
        Return _Server
    End Get
    Set
        _Server = Value
    End Set
End Property

Your function that gets the connection string needs to be renamed, it is not connecting the server all by itself, it is returning a connection string. I suggest: GetConnectionString or something like that

your function that gets the connection string needs to be renamed, it is not connecting the server all by itself, it is returning a connection string. I suggest: GetConnectionString or something like that

You created public properties for

  • UserName
  • Password
  • Server
  • Database

but you shouldn't allow these things to be editable by other objects

these are things that need to be hidden from everyone except this class, the consumer of this class should not know what these values are, or be allowed to change them without following a strict set of rules defined in the class itself

I think that what you want is a protected property, something that is only available to the class and any class that inherits the class.

the Server property would look like this:

Protected Property Server
    Get
        Return _Server
    End Get
    Set
        _Server = Value
    End Set
End Property

Your function that gets the connection string needs to be renamed, it is not connecting the server all by itself, it is returning a connection string. I suggest: GetConnectionString or something like that

Source Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188

your function that gets the connection string needs to be renamed, it is not connecting the server all by itself, it is returning a connection string. I suggest: GetConnectionString or something like that

I also believe that this can be done in a single line, and if you can use string interpolation you can get rid of those pesky ampersands too.

Your code:

Public Function Connect_Server() As String
    Connect_Server = "server=" & Server & ";" & "userid=" & UserName & ";" & "password=" & Password & ";" & "database=" & Database
    Return Connect_Server
End Function

after my suggestions:

Public Function GetConnectionString() As String
    Return $"server = {Server};userid={UserName};password{Password};database{Database}"
End Function

And then you can use some using statements to clean up the clean up of the Retrieve_Row function

by doing this you won't have to call the Garbage Collector or close your Connection, Command or Reader. you can also assign the properties of the command with the Object Initializer syntax.

so now the code looks like this:

Public Function Retrieve_Row(SQLStatement As String) As ArrayList
    Try
        Dim result As New ArrayList
        Using MySqlConnection As New MySqlConnection With {.ConnectionString = GetConnectionString()}
            MySqlConnection.Open()
            Using MySqlCommand As New MySqlCommand _
                With {.CommandText = SQLStatement,
                            .Connection = MySqlConnection}
                Using reader = MySqlCommand.ExecuteReader
                    While reader.Read()
                        Dim Dict As New Dictionary(Of String, Object)
                        For count As Integer = 0 To (reader.FieldCount - 1)
                            Dict.Add(reader.GetName(count), reader(count))
                        Next
                        result.Add(Dict)
                    End While
                End Using
            End Using
        End Using
        Return result
    Catch ex As Exception
        Console.WriteLine("MySQL retrieve row: " & ex.Message & Err.Number)
    End Try
End Function