Check for either ADO.NET / OLEDB Connection presence

我只是一个虾纸丫 提交于 2019-12-13 17:19:07

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/11216494/check-for-either-ado-net-oledb-connection-presence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!