DEV Community

Discussion on: How to Send a Complex Type List of Objects to SQL Server

Collapse
 
peledzohar profile image
Zohar Peled • Edited

Good article! A couple of important notes, though:

  1. When using a ADO.Net with a table valued parameter, the order of the columns in the data table you're sending to the database must match the order of the columns defined in the user defined table type.

  2. table valued parameters are always a better choice than delimited strings, even if the type is not complex (i.e. a list of ints etc.)

  3. Instead of AddWithValue (see Can we stop using AddWithValue() already? you can simply use Add:

sqlCommand.Parameters.Add("@MovieAssignments", SqlDbType.Structured).Value = movieAssignments;
Collapse
 
edwinoaragon profile image
Edwin Aragon

Thank you for your reply.

The first thing you mention in your reply is worth adding to the post, I'll update it right away.

About point 2, I think the same, it's better to take advantage of strongly typed things when possible since you can detect bugs earlier in your development process.

I didn't know about point 3, super useful information thank you for bringing in it up!

I think we are on the same page though since in the next line I'm assigning the SQL type explicitly through:

movieAssignmentsParameter.SqlDbType = SqlDbType.Structured;

So I think it does the same as your line, please correct me if I'm wrong.

Collapse
 
peledzohar profile image
Zohar Peled

I'm glad I could help.
About AddWithValue - yes, you can use it like you've done.
Personally, I find using Add more readable - but that's probably a personal preference.
Cheers!