1、存在问题的代码:
foreach (DataColumn ukColumn in ukColumns){
string sqlstr = "select *** from tablename where ***";
OleDbCommand command = new OleDbCommand(sqlstr, bitsConn);
OleDbDataReader reader = command.ExecuteReader();
reader.Read();
typestr = reader[0].ToString().Substring(0, 3);
*****
reader.Close();
}
2、出错点:OleDbDataReader reader = command.ExecuteReader(); 错误信息:游标溢出。
3、解决方案一:
增加代码:command.dispose();
4、解决方案二:
使用代码:useing(OleDbCommand command = new OleDbCommand(sqlstr, bitsConn);)
using 语句允许程序员指定使用资源的对象应当何时释放资源。为 using 语句提供的对象必须实现 IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源。
string sqlstr = "select nvl(max(TYPE),0) from BITSMETACOLUMN where lower(ENAME)=lower('" + ukColumn.ColumnName + "') and METATABLEID=(select ID from BITSMETATABLE where lower(ENAME)='" + tableName.ToLower() + "')";
OleDbCommand command = new OleDbCommand(sqlstr, bitsConn);
OleDbDataReader reader = command.ExecuteReader();
reader.Read();
if (reader[0].ToString() == "0")
{
throw new Exception("基表【BITSMETACOLUMN】UK定义异常:sqlstr=" + sqlstr);
}
typestr = reader[0].ToString().Substring(0, 3);
reader.Close();foreach (DataColumn ukColumn in ukColumns)