0

It's been helpfully suggested that I don't have to keep writing

dr["building_id"].ToString()

in the code snipped below. That I only need to put the above into a variable, i.e, sBldgID, and use the latter subsequently.

...
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);

...
            }
         }
      }
   }
}
...

Good advice. How do I do it? How do I use dr.Read() that way before the while loop starts? Thanks.

2 Answers 2

1

I guess the advice was pretty clear:

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);

...
            }
         }
      }
   }
}

Simply chache the dr["building_id"].ToString() in a local variable (like building_id here).

Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't even that itself keep getting performed every time it loops? Or is that pretty much the shortest route?
@ni37osllb yes of course...every dr.Read() switches to the next row in the result, and I assume you want to use the building_id of this next row. Or is this building id the same in all rows?
For better readability I suggest using if (!String.IsNullOrWhiteSpace(buildingId)) instead of 3 comparisons.
@derpirscher I'd agree, but don't know OP's detailed requirements, maybe it's not the same
René, building_id is different for other rows, thanks. And thanks all.
1

Assuming building_id is numeric, I instead recommend you write:

if (! System.Convert.IsDbNull(dr["building_id"]))  

If it's text, I'd recommend this:

if (! System.Convert.IsDbNull(dr["building_id"]) && ! String.IsNullOrWhiteSpace(dr["building_id"].ToString()))

This is how we check for nulls, blanks and whitespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.