rvphx
rvphx

Reputation: 2402

Check for either ADO.NET / OLEDB Connection presence

I am developing a custom SSIS component for use across my entire company. Right now the code (which is from here) accepts only ADO.NET connection type.

I would like to support OLEDB type as well and would like to change my code accordingly. The piece of code that checks for valid ADO.NET connectivity is:

SqlConnection connection = connections[_connectionName].AcquireConnection(null) as SqlConnection;

     if (connection == null)
          {
          componentEvents.FireError(0, METHOD_NAME, "The connection is not a valid ADO.NET connection", "", -1);
          return DTSExecResult.Failure;
          }

This would just check for a valid ADO.NET connection. How would I change this to check for OLEDB connection ALSO. So for example, if the connection type is OLEDB, it should be accepted, if its neither of those, it should fail.

I am not much of a C# person and hence I am looking for any help that I can get on this. Thanks for any help.

Upvotes: 1

Views: 333

Answers (1)

Kevin Aenmey
Kevin Aenmey

Reputation: 13419

You can use the is keyword to determine if an object is an instance of a specified type (or a type that derives from the specified type). see MSDN

var connection = connections[_connectionName].AcquireConnection(null);

if (!(connection is SqlConnection || connection is OleDbConnection))
{
     componentEvents.FireError(0, METHOD_NAME, "The connection is not a valid ADO.NET connection", "", -1);
          return DTSExecResult.Failure;
}

If you want to determine if the connection is any DbConnection type (from which both SqlConnection and OleDbConnection derive), you could do the following:

DbConnection connection = connections[_connectionName].AcquireConnection(null) as DbConnection;

if (connection == null)
{
    componentEvents.FireError(0, METHOD_NAME, "The connection is not a valid ADO.NET connection", "", -1);
    return DTSExecResult.Failure;
}

Upvotes: 2

Related Questions