【问题标题】:Member of non-managed class cannot be a handle非托管类的成员不能是句柄
【发布时间】:2017-01-31 02:38:53
【问题描述】:

我正在尝试使用 C++ 中的 SqlClient 设置 SQL 连接。

我遇到了以下错误

“非托管类的成员不能是句柄”

    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Data.dll>
    #using <System.Xml.dll>
    using namespace std;

    using namespace System;
    using namespace System::Data;
    using namespace System::Data::SqlClient;

    class CConnection {

    public:
    SqlConnection ^connection=gcnew SqlConnection();

    CConnection(String^,String^);

    void close();
    void connect(String^,String^);
    bool isConnected();
    SqlConnection getCon();

    ~CConnection();

};

错误发生在以下行SqlConnection connection=gcnew SqlConnection();

有什么想法吗?

【问题讨论】:

    标签: sql database visual-studio c++-cli


    【解决方案1】:

    您仅使用 class 关键字将 CConnection 声明为非托管类。但是,CConnection 大量使用了托管功能:它有一个类型为 SqlConnection 的成员,这是一个托管类,并且您在类中使用了句柄 (^) 而不是指针 (*)。您可能希望通过使用 ref class 而不是仅使用 classCConnection 声明为托管类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-19
      • 2012-05-18
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      相关资源
      最近更新 更多