Search code examples
c++mysqlwinformsuser-interfaceclr

a handle to a non-managed class is not allowed


I'm trying to make a connection between c++ windows form and mysql database, but it show this error to me.

a handle to a non-managed class is not allowed

        #pragma endregion
        private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
            std::string^ constring =L "datasource=localhost;port=**;username=root;password=**";
            MySqlConnection^ conDataBase=gcnew MySqlConnection(constring);
            MySqlCommand^ cmdDataBase=gcnew MySqlCommand("",conDataBase);
        }
    };
}

Solution

  • std::string is the string class from the C++ Standard Library. You cannot use it with ^ (i.e., as a managed class).

    Instead, use the .NET String class:

    System::String^ constring = L"datasource=localhost;port=**;username=root;password=**";