0

I have a project that is CLS-Compliant except one variable declaration which is confusing me how to correct.

I have a class called MySQL, summed up it looks like:

public class mysql
 implements idisposable

 private connection as new mysqlconnection
 private command as new mysqlcommand
 <CLSCompliant(false)> public reader as mysqldatareader

 public sub new(byval query as string)
  if connection.state = connectionstate.open then
    connection.connectionstring = my.settings.connectionstring
    connection.open()

    command.connection = connection
    command.commandtext = query
 end sub

 public sub parameters(byval name as string, byval value as string)
  command.parameters.addwithvalue(name, value)
 end sub

 public sub retrieve()
  reader = command.executereader()
 end sub

 public sub send()
  command.executenonquery()
 end sub

 public sub close()
  if me.command.connection.state = connectionstate.open then me.command.connection.close
  if me.connection.state = connectionstate.open then me.connection.close
  me.dispose()
 end sub

 #region "IDisposable support"
  private disposedvalue as boolean
   protected overridable sub dispose(disposing as boolean)
     if not me.disposedbalue then
       if disposing then
       #dispose maaged state
       end if

       connection.dispose()
       command.dispose()
       reader = nothing
      end if
      me.disposedvalue = true
   end sub

  public sub dispose() implement idisposable.dispose
    dispose(true)
    gc.suppressfinalize(me)
  end sub
 #end region

end class

usage:

dim sql as new MySQL("SELECT * FROM TABLE WHERE NAME = ?Name")
sql.parameters("?Name", me.textboxusername.text)

call sql.retrieve()

while sql.reader.read
  text = sql.reader.getstring(sql.readergetordinal("User_Name")
end while

call sql.close()

Public reader as MySQLDataReader is not CLS-Compliant and I am not sure how to correct the issue. I have upgraded to the latest mysql data connector version. I am wondering if I should make it a private variable and read the results threw a function?

I apologize for miss-capitalization, miss-spelling, etc. in the following code snippet, I typed it while viewing it from a remote desktop connection.

Jeff
  • 1,609
  • 3
  • 20
  • 25

1 Answers1

2

Try this code sir for declaring mysqlcommand .

    Public query As String, cmd As MySqlCommand, reader As MySqlDataReader
    Public SQLConnection As MySqlConnection = New MySqlConnection
Dreamer XD
  • 208
  • 3
  • 15
  • How would that work for retrieving my data in various functions though? Ie: reader.getstring.getordinal("columnname") ? – Jeff Feb 07 '14 at 01:38
  • I delclare by sql as new mysql("select whatever") .. mysql.retrieve .. while reader.read ... Text = sql.reader.getstring(sql.reader.getordinal("column")) – Jeff Feb 07 '14 at 01:40
  • I edited my originally question to include the full class - I have it setup this way to pass parameters as well. – Jeff Feb 07 '14 at 01:55
  • query = " Select * from table WHERE name= '" + name+ "'" mysql_connect(True) cmd = New MySqlCommand(query, SQLConnection) reader = cmd.ExecuteReader() Try If reader.Read() Then 'function else 'function end if – Dreamer XD Feb 07 '14 at 02:49