c# - Int32.TryParse() or (int?)command.ExecuteScalar() -


I have a SQL query that gives only one field - an IID type INT.

And I have to use it as an integer in the C # code.

How is fast and uses less memory?

  int id; If (int32.TryParse (command.ExecuteScalar (). ToString (), outside ID)) {// use id}  

or

  int? Id = (int?) Command.exequists (); If (id.HasValue) {// use id.Value}  

or

  int? Id = command.ExecuteScalar () as int?? If the difference between (id.HasValue) {// use id.Value}  

According to the three demonstrations, the impedance is taking the data from DB to your app, not a trivial cast or method call.

I go with it:

  int? Id = (int?) Command.exequists (); If (id.HasValue) {// use id.Value}  

it first fails , if one day people return a string or a date To change the order, at least it will crash and you will get a chance to fix it.

I would also go with a simple int cast if command to return the same result was expected

Note, I generally prefer to return an externally compared to the executed scalar, execute the scalar looks delicate (the conference is that the first column in the first row does not fit a return value).


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -