有人建议我不必继续写
下方代码中的dr [“ building_id”]。ToString()
。我只需要将上面的内容放入变量sBldgID,然后再使用该变量即可。
...
using (OracleCommand cmd = new OracleCommand(sql, conn)) {
cmd.CommandType = CommandType.Text;
using (OracleDataReader dr = cmd.ExecuteReader()) {
while (dr.Read()) {
if (dr["building_id"].ToString() != " " && dr["building_id"].ToString() != "" && dr["building_id"].ToString() != null) {
using (SpreadsheetDocument document = SpreadsheetDocument.Open(path, true)) {
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == dr["building_id"].ToString());
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.FirstOrDefault().Id);
...
}
}
}
}
}
...
好的建议。我该怎么做?在while循环开始之前,如何使用dr.Read()?谢谢。
答案 0 :(得分:1)
我想建议很明确:
using (OracleCommand cmd = new OracleCommand(sql, conn)) {
cmd.CommandType = CommandType.Text;
using (OracleDataReader dr = cmd.ExecuteReader()) {
while (dr.Read()) {
string buildingId = dr["building_id"].ToString(); // <-- chache building id
if (buildingId != " " && buildingId != "" && buildingId != null) {
using (SpreadsheetDocument document = SpreadsheetDocument.Open(path, true)) {
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == buildingId);
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.FirstOrDefault().Id);
...
}
}
}
}
}
只需将dr["building_id"].ToString()
放在局部变量中(如此处的building_id
)。
答案 1 :(得分:1)
假设building_id为数字,我建议您写:
if (! System.Convert.IsDbNull(dr["building_id"]))
如果是文字,我建议这样做:
if (! System.Convert.IsDbNull(dr["building_id"]) && ! String.IsNullOrWhiteSpace(dr["building_id"].ToString()))
这是我们检查null,空格和空格的方式。