我有一个符合 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 数据连接器版本。我想知道是否应该将其设为私有变量并读取结果抛出一个函数?
对于以下代码片段中的大写错误、拼写错误等,我深表歉意,我是在从远程桌面连接查看时输入的。