Is it easy to convert SqlCommand to a T-SQL string?

I have a populated SqlCommand object containing command text and parameters of various types of databases, as well as their values. I need a T-SQL script that I could just execute, which would have the same effect as calling the ExecuteNonQuery method on the command object.

Is there an easy way to make such a "script dump" or do I need to manually create such a script from a command object?

+4
source share
1 answer

I do not think you can get the full sql statement from the SqlCommand object directly. If you register all the commands sent to your sql server, you will see that the transmitted command text matches the command text of your SqlCommand object. Parameter values ​​are transferred separately.

So, I think that the only option would be to collect the team text yourself. This is pretty easy; You start with the CommandText SqlCommand and iterate through the Parameters collection, replacing the parameter holders with (shielded) parameter values. This should result in a full sql query that does what you are after.

+4
source

Source: https://habr.com/ru/post/1305524/


All Articles