What's new
Carbonite

South Africa's Top Online Tech Classifieds!
Register a free account today to become a member! (No Under 18's)
Home of C.U.D.

C# with SQL Command Params - How the heck is this working?

iamgigglz

VIP
VIP Supporter
Rating - 100%
311   0   0
Joined
Aug 19, 2014
Messages
8,684
Reaction score
2,335
Points
10,155
Location
Parkhurst
The way I see it, the parameter should be created, given a value (and a direction in this case) and THEN added to the command.
The code below is doing it out of order but seems to be working, and it's hurting my brain. I want to change the sequence but don't want to "fix what ain't broke".

[CODE lang="csharp" title="This shouldn't work, but does"]cmdParameter = new SqlParameter("@strInventoryStatus ", SqlDbType.NVarChar);
cmdParameter.Direction = ParameterDirection.Input;
cmdCommand.Parameters.Add(cmdParameter);
cmdParameter.Value = GRVLDetails.strStatus;

cmdParameter = new SqlParameter("@strInventoryClassification", SqlDbType.NVarChar);
cmdParameter.Direction = ParameterDirection.Input;
cmdCommand.Parameters.Add(cmdParameter);
cmdParameter.Value = GRVLDetails.strClassification;

cmdParameter = new SqlParameter("@strQCStatus", SqlDbType.NVarChar);
cmdParameter.Direction = ParameterDirection.Input;
cmdCommand.Parameters.Add(cmdParameter);
cmdParameter.Value = GRVLDetails.strQCStatus;[/CODE]
 
Changing the sequence will still work.
cmdParameter obj already lives in memory, the Value is being updated by reference.

Don't like this pattern though. It's a long ceremony.

If you can, use Dapper ORM, it's closest to ado.net perf-wise and does the parameter mapping automagically.
like the above would be translated to
new {
strInventoryStatus = GRVLDetails.strStatus,
strInventoryClassification = GRVLDetails.strClassification,
strQCStatus = GRVLDetails.strQCStatus
}
 
You can condense the 4 lines into 1, dont need all that steps.

sqlCmd.Parameters.Add("@strInventoryStatus", SqlDbType.NVarChar).Value = GRVLDetails.strQCStatus;
 
It should work, there is nothing wrong. The sequencing is only preference. When you add the parameter to the list, a reference to the actual parameter object is stored in the list, not the object itself. Meaning if you change the object after adding it to the list, the changes will reflect perfectly in your list (because a reference is stored in the list, not the actual object).
Also, only when you execute the command will the parameter values and everything be used, so the sequencing of setting properties and so on does not matter until you execute the command, because it is not used before that.

I hope that makes sense? Maybe read up on object references and pass by value VS pass by reference. Then this will make more sense.


[CODE lang="csharp" title="This also works"]cmdParameter = new SqlParameter("@strInventoryStatus ", SqlDbType.NVarChar);
cmdCommand.Parameters.Add(cmdParameter);
cmdParameter.Direction = ParameterDirection.Input;
cmdParameter.Value = GRVLDetails.strStatus;[/CODE]
 

Users who are viewing this thread

Latest posts

Back
Top Bottom