我有一个符合CLS标准的项目,除了一个变量声明,令我困惑如何纠正。
我有一个名为MySQL的类,总结起来看起来像:
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
用法:
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()
作为MySQLDataReader的公共读者不符合CLS,我不知道如何纠正这个问题。我已升级到最新的mysql数据连接器版本。我想知道我是否应该把它变成一个私有变量并读取结果抛出一个函数?
我为以下代码片段中的错误大写,拼写错误等道歉,我在从远程桌面连接查看时输入了它。
答案 0 :(得分:2)
尝试使用此代码先生来声明mysql命令。
Public query As String, cmd As MySqlCommand, reader As MySqlDataReader
Public SQLConnection As MySqlConnection = New MySqlConnection