C++ 将值从visual studio c+插入数据库+;2010年,使用键盘上的值。。。?

C++ 将值从visual studio c+插入数据库+;2010年,使用键盘上的值。。。?,c++,mysql,visual-studio-2010,visual-studio-2012,C++,Mysql,Visual Studio 2010,Visual Studio 2012,好的,现在我有了这个代码: namespace testtesttest { using namespace System; using namespace System::Data; class Forme { string name; public: Forme( char *_name ) : name(_name) { } ~Forme() { } void save() { String

好的,现在我有了这个代码:

namespace testtesttest {

using namespace System;
using namespace System::Data;

class Forme
{
    string name;
public:

    Forme( char *_name ) : name(_name)
    {
    }

    ~Forme()
    {
    }

    void save()
    {

        String^ constring = L"datasource=localhost;port=3306;username=root;password=root";
        MySqlConnection^ conDataBase = gcnew MySqlConnection( constring );

        string temp(MySqlCommand^ cmdDataBase = gcnew MySqlCommand( " insert into test.first (NAME) values ('"+this->name+"');" ) );
        String^ myInter = gcnew String( temp.c_str() );
        MySqlCommand^ cmdDataBase = gcnew MySqlCommand(myInter, conDataBase);

        MySqlDataReader^ myReader;

        try{
            conDataBase->Open();
            myReader = cmdDataBase->ExecuteReader();

            cout << endl << "SAVE!";

            while( myReader->Read() )
            {

            }
            } catch( Exception^ex) {
                                   }

    }
};
};
名称空间测试{
使用名称空间系统;
使用名称空间System::Data;
形式类
{
字符串名;
公众:
Forme(char*\u name):名称(\u name)
{
}
~Forme()
{
}
作废保存()
{
字符串^consting=L“数据源=localhost;端口=3306;用户名=root;密码=root”;
MySqlConnection^conDataBase=gcnewmysqlconnection(constring);
字符串temp(MySqlCommand^cmdDataBase=gcnew MySqlCommand(“插入test.first(NAME)值(““+this->NAME+”);”));
字符串^myInter=gcnew字符串(temp.c_str());
MySqlCommand^cmdDataBase=gcnewmysqlcommand(myInter,conDataBase);
MySqlDataReader^myReader;
试一试{
conDataBase->Open();
myReader=cmdDataBase->ExecuteReader();
cout test.cpp(34):错误C2587:“this”:非法使用局部变量作为默认参数

1> cpp(29):参见“this”的声明

1> cpp(34):错误C2227:“->name”的左边必须指向class/struct/union/generic类型

1> cpp(35):错误C2228:“.c_str”的左边必须有class/struct/union

1>

1> 生成失败


我假设您在这行中得到了错误:

MySqlCommand^ cmdDataBase = gcnew MySqlCommand( " insert into test.first (NAME) values ('"+this->name+"');", conDataBase );
使用c样式的字符串,您不能使用
+
将它们连接起来。相反,您需要使用类似
strcat
sprintf
的函数

char temp[300];
sprintf(temp, " insert into test.first (NAME) values ('%s');", this->name);
MySqlCommand^ cmdDataBase = gcnew MySqlCommand(temp, conDataBase);
<> p>通常使用C++风格字符串为:

class Forme
{
    std::string name;
public:
    Forme( char *_name ) : name(_name)
    {
    }

void save( )
    {
       ...
       std::string temp(" insert into test.first (NAME) values ('"+this->name+"');");
       MySqlCommand^ cmdDataBase = gcnew MySqlCommand(temp.c_str(), conDataBase);
       ...
    }
编辑: 对于您的后续问题:
这实际上是我文章中的一个错误。我已更正。

尝试使用cmd而不是cmdDataBase。 并添加以下内容“使用名称空间MySql::Data::MySqlClient;”
如果您在visual studio 10及更高版本中使用mysql 5.6及更高版本进行编码,谢谢您的帮助。然后您需要将char*转换为String^以使其正常工作。如果没有看到
MySqlCommand
的代码,很难知道会发生这种情况。当我尝试转换时,我得到了这样的结果:“this”:非法使用局部变量作为默认参数meter 1>testtest.cpp(68):参见'this'1>testtest.cpp(73)的声明:错误C2227:name的左边必须指向class/struct/union/generic type 1>testtesttest.cpp(74):错误C2228:the.c_str的左边必须有class/struct/union…代码是字符串temp(MySqlCommand^cmdDataBase=gcnew MySqlCommand)(“insert into test.first(名称)值(““+this->NAME+”);”);字符串^myInter=gcnew字符串(temp.c_str());MySqlCommand^cmdDataBase=gcnew MySqlCommand(myInter,conDataBase);抱歉,我无法很好地理解。请编辑到您的问题中,或创建一个新的问题