IndentedTextWriter tw = new IndentedTextWriter(internalTW, " ");
'tw'引用的对象丢失,但相关资源未在此处处理“TW”是文本编写者,其中internalTW是TextWriter
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(cmd1, con);
'cmd'引用的对象已丢失,但相关资源未在此处处理
答案 0 :(得分:3)
尝试
using (IndentedTextWriter tw = new IndentedTextWriter(internalTW, " ")) {
// use it here
}
RESP。
using (OleDbConnection con = new OleDbConnection(conStr))
using (OleDbCommand cmd = new OleDbCommand(cmd1, con)) {
// use it here
}
在using
块的末尾,对象调用Dispose()
并释放资源...
答案 1 :(得分:3)
所有类型都会实现IDisposable
,因此是调用者的责任,例如调用Dispose()
。
using(var tw = new IndentedTextWriter(internalTW, " ")) {
// do something with tw
}
或在Dispose()
块中明确调用finally
。