views:

184

answers:

2

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.

Thanks!

A: 

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.

Will
There is a similar method for TableAdaptors of strongly typed DataSets?
CrashTECH
I'd assume its exactly the same, as these strongly typed data adapters are simply regular adapters that VS has wrapped (via a tool) inside of a type safe class. If the type safe wrapper doesn't extend from IDbDataAdapter you might have to add the methods manually to access the internal adapter.
Will
A: 

Would something like this work for the a strongly typed data set with table adaptors?

if (SqlCommandObj.GetType().Name.Contains("TableAdapter"))
 {
     sqlCmdInsert = (SqlCommand)(SqlCommandObj.GetType().GetField("InsertCommand").GetValue(SqlCommandObj));
 }
CrashTECH