![]() ![]() I thought, geez, I must be missing something. In fact, that is why I looked so hard when I first saw this code. Is this basic stuff you already know? Very likely (or hell yeah!). Use ExecuteScalar to execute a query to return a single scalar value and use ExecuteNonQuery to execute an action query and return no values. So I started to refactor the code to change the places that were using ExecuteScalar but not actually getting a return value into code like this: That's not psuedo code, there was nothing on the left hand side to store the return value of the ExecuteScalar method. most of which were also issuing a simple action query with no intention of getting a result back. So why was this code using ExecuteScalar to run this SQL? I started looking around the project that I inherited and found several other places that ExecuteScalar was used. string sql = "UPDATE tableX SET columnA = value1 WHERE columnB = value2" ![]() This code was simply doing something like this: So then I wondered if the UPDATE statement was also returning a value after the UPDATE. At first I though, maybe the code was actually returning a value and not executing an UPDATE statement, so I looked at it again thinking I missed something. It worked just fine and dandy, but I had to take a second look because the code was issuing the ExecuteScalar method. But recently I worked on a project that just made me think "WTF"? I saw some ADO.NET code that was intended to execute an UPDATE statement against a table in a SQL Server database. ![]() This might seem like an obvious point to most, and I sure hope it does. Just hammer the nail into the wall. OK, need something more code related? Would you define a class with a single property and no other members just to store a string? Me neither. Would you use a drill to pre-drill a hole for a nail to hang a picture? I wouldn't either. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |