Why MSDN deprecates the AddWithValue method

nqyuta 80 Reputation points
2025-02-07T08:02:47.0133333+00:00

I am trying to connect to a SQL Server table and update or register data in a program created in C#.

In order to create a program that can take measures against SQL injection and support N prefixes, I am thinking of using the AddWithValue method, a method that belongs to the Parameters property of the SqlCommand object.

However, the AddWithVaue method is deprecated in MSDN.

What problems will occur if I use the AddWithValue method?

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
SQL Server | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 128.3K Reputation points MVP Volunteer Moderator
    2025-02-07T21:51:10.9+00:00

    AddWithValue is evil, and you should absolutely not use it. From the naïve C# programmer's perspective, it may seem slick, but it is a disaster on the database side.

    Dan Guzman explains this very well in his blog post https://www.dbdelta.com/addwithvalue-is-evil/

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.