Is it possible to get the parsed text of a SqlCommand with SqlParameters?

后端 未结 4 2064
情话喂你
情话喂你 2020-12-11 02:00

What I am trying to do is create some arbitrary sql command with parameters, set the values and types of the parameters, and then return the parsed sql command - with parame

4条回答
  •  猫巷女王i
    2020-12-11 02:27

    You have a mistaken notion of how parameterized queries work. The "parsed text" you speak of is never created, and parameter values are never substituted directly into the query string.

    That's why it's so important to use parameterized queries — you have complete segregation of query data from query code. Data is data, code is code, and never the twain shall meet. Thus, there is no possibility for sql injection.

    What it means is that if you have a CommandText like this:

    SELECT SomeColumn FROM SomeTable WHERE ID= @ID
    

    instead of ultimately running a query that looks like this:

    SELECT SomeColumn FROM SomeTable WHERE ID= 123
    

    you actually run something more like this:

    DECLARE @ID Int
    Set @ID = RetrieveQueryDataItem("@ID")
    SELECT SomeColumn FROM SomeTable WHERE ID= @ID
    

    Now, this isn't exactly what happens; the engine doesn't transform the code like that. Instead, it uses the sp_executesql procedure. But this should help you understand what's going on.

提交回复
热议问题