How would you get the Sql Command objects for a Given TableAdaptor and SqlDataAdaptor in C# (.NET 2.0)
-
01-07-2019 - |
Question
I am creating a generic error handling / logging class for our applications. The goal is to log the exception info, info about the class and function (as well as parameters) and if relevant, the information about the System.Data.SqlClient.SqlCommand
object.
I would like to be able to handle passing in SqlCommands
, TableAdaptors
, and SqlDataAdaptors
.
I am new to using reflection and I know that it is possible to do this, I am just not sure how to go about it. Please advise.
Solution
Is this what you're talking about?
SqlDataAdapter da = new SqlDataAdapter();
var cmd1 = ((IDbDataAdapter)da).DeleteCommand;
var cmd2 = ((IDbDataAdapter)da).UpdateCommand;
var cmd3 = ((IDbDataAdapter)da).SelectCommand;
var cmd4 = ((IDbDataAdapter)da).InsertCommand;
The SqlDataAdapter implements IDbDataAdapter, which has getters/setters for all the CRUD commands. The SqlDataAdapter implements these explicitly, so they don't show up in the signature of the class unless you first cast it to the interface. No reflection necessary.