检查无论是ADO.NET / OLEDB连接presence [英] Check for either ADO.NET / OLEDB Connection presence

查看:181
本文介绍了检查无论是ADO.NET / OLEDB连接presence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在我的整个公司使用一个定制的SSIS组件。眼下code(这与这里)只接受ADO。 NET连接类型。

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.

我想支持OLEDB类型以及并希望随之改变我的code。这件作品的code,检查对有效ADO.NET连接是:

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

这将只检查一个有效的ADO.NET连接。我将如何改变这种检查OLEDB连接也。因此,例如,如果连接类型是OLEDB中,它应该被接受,如果其既不那些,应该失败。

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.

我倒没一个C#的人,所以我期待您的帮助,我可以在此得到。感谢您的帮助。

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.

推荐答案

您可以使用关键字来确定一个对象是指定类型的实例(或派生于指定类型的类型)。 请参阅MSDN

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

如果你想确定是否连接任何的DbConnection 键入(从两个的SqlConnection 的OleDbConnection 导出),你可以做到以下几点:

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

这篇关于检查无论是ADO.NET / OLEDB连接presence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆