您好我正在制作一个Visual c ++应用程序,但是当我尝试将数据写入数据库时,它只在主键中写入“0”,其余为空值,但我很确定我从文本框中获取数据,所以我不知道发生了什么。 以下是“添加”按钮事件的代码:
private: System::Void btn_add_Click(System::Object^ sender, System::EventArgs^ e) {
Worker^ pwork = gcnew Worker();
pwork->dni = Int32::Parse(txt_dni->Text);
pwork->lastname = txt_lastname->Text;
pwork->slastname = txt_slastname->Text;
pwork->fname = txt_name->Text;
pwork->sname = txt_sname->Text;
pwork->post = txt_post->Text;
WorkerManager^ manager = gcnew WorkerManager();
manager->AddWorker(pwork);
RefreshWorkerListBox();//this doesn't matter
}
我知道在进入“AddWorker”之前,'pwork'包含我想要的所有信息,我已经检查过了,但在AddWorker中我不知道发生了什么,请在这里提供一些帮助:
void WorkerManager::AddWorker(Worker^ pwork){
MySqlConnection^ conn = nullptr;
conn = gcnew MySqlConnection();
conn->ConnectionString = "Database=produccion;Data Source=localhost;User Id=root;Password=*******";
conn->Open();
//Sentencia
MySqlCommand^ comm = gcnew MySqlCommand();
comm->CommandText = "INSERT INTO personalsfc " + "VALUES(dni,lastname,slastname,name,sname,post)";
MySqlParameter^ p0 = gcnew MySqlParameter("dni", MySqlDbType::Int32);
MySqlParameter^ p1 = gcnew MySqlParameter("lastname", MySqlDbType::VarChar);
MySqlParameter^ p2 = gcnew MySqlParameter("slastname", MySqlDbType::VarChar);
MySqlParameter^ p3 = gcnew MySqlParameter("name", MySqlDbType::VarChar);
MySqlParameter^ p4 = gcnew MySqlParameter("sname", MySqlDbType::VarChar);
MySqlParameter^ p5 = gcnew MySqlParameter("post", MySqlDbType::VarChar);
p0->Value = pwork->dni;
p1->Value = pwork->lastname;
p2->Value = pwork->slastname;
p3->Value = pwork->fname;
p4->Value = pwork->sname;
p5->Value = pwork->post;
comm->Parameters->Add(p0);
comm->Parameters->Add(p1);
comm->Parameters->Add(p2);
comm->Parameters->Add(p3);
comm->Parameters->Add(p4);
comm->Parameters->Add(p5);
comm->Connection = conn;
comm->ExecuteNonQuery();
conn->Close();
}