Dapper and SQL Injections

前端 未结 2 1312
南方客
南方客 2020-12-02 18:36

How does Dapper help protect against SQL injections? I am testing out different DAL technologies and have to choose one to be secure our site. I\'m leaning towards Dapper (h

2条回答
  •  广开言路
    2020-12-02 19:11

    You just need to use parameterized queries like you always should. Since Dapper is just a "tiny" (and pretty thin) extension to "raw" SQL and ADO.NET - just use parameterized ADO.NET queries and supply parameters.

    See this sample from the Dapper-Dot-Net site:

    var dog = connection.Query("select Age = @Age, Id = @Id", 
                                    new { Age = (int?)null, Id = guid });
    

    The SQL query uses parameters - and you supply those to the "Dapper" query.

    To summarize: using Dapper in itself doesn't help protect against SQL injections per se - using parameterized ADO.NET/SQL queries however does (and those queries are absolutely supported by Dapper, no issues at all)

提交回复
热议问题