我有这样的代码:
DataSet QtyDS = null;
. . .
QtyDS = GetAllUPCDSDRecords(txtUPC.Text);
...那是因为“找不到表 0”而爆炸
为了防止这种情况发生,我尝试了以下方法,但均无济于事;当我尝试访问数据集中的第一个表时,我仍然收到该错误消息:
1)
if (null != QtyDS)
2)
string table0 = QtyDS.Tables[0].ToString();
if (!table0.Equals(string.Empty))
3)
if (null != QtyDS.Tables[0])
如何安全地确定查询是否返回数据集以避免错误消息?
更新
public DataSet getAllUPCDSDRecords(string upc)
{
string query = string.Format(
"SELECT tyger_id as tyger, upc_source as UPC, description as Descrip, unit_qty as Qty, "+
"department as Dept, vendor_id as Ven, upc_pack_size as UPCPK, pack_size as PKSize, "+
"unit_cost as Cst, unit_list as Lst "+
"FROM {0} WHERE upc_source = {1}", tablename, upc);
return dbconn.getDataSet(query);
}
public DataSet getDataSet( string dynSQL )
{
checkConnection();
SqlCeDataAdapter oDA = new SqlCeDataAdapter( dynSQL, objCon );
DataSet oDS = new DataSet( "Command" );
try
{
oDA.Fill( oDS );
}
catch
{
//SSCS.ExceptionHandler(ex, "DBConnection.getDataSet");
}
return( oDS );
} // getDataSet