Why MSDN deprecates the AddWithValue method

那由多 0 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?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,422 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,456 questions
C#
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.
11,288 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 116.6K Reputation points MVP
    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 Answers by the question author, which helps users to know the answer solved the author's problem.