m0_46820994
2020-08-18 21:35
采纳率: 57.1%
浏览 119

向数据库写入数据出错,“System.Data.OleDb.OleDbException”类型的未经处理的异常在 System.Data.dll 中发生 其他信息: 查询值的数目与目标字段中的数目不同。

public: System::Void OpenAccess()

{
String^ oleDBString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Office2019\MyProject\sensor.accdb;";
OleDbConnection^ os = gcnew OleDbConnection(oleDBString);
String^ sql = "select* from Sensor1";
DataSet^ ds = gcnew DataSet();
OleDbDataAdapter^ oleDbDataAdapter = gcnew OleDbDataAdapter(sql, os);
oleDbDataAdapter->Fill(ds);
dataGridView1->DataSource = ds->Tables[0]->DefaultView;
os->Close();
}
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
String^ text1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Office2019\MyProject\sensor.accdb;";
String^ text2 = "Insert Into Sensor1(Temperature,Humidity)Values(";
text2 += textBox4->Text + ",";
text2 += textBox6->Text + ")";
OleDbConnection^ conn = gcnew OleDbConnection(text1);
conn->Open();
OleDbCommand^ da = gcnew OleDbCommand();
da->CommandText = text2;
da->Connection = conn;
da->ExecuteNonQuery(); // 出错行!!!!
textBox4->Text = "";
textBox6->Text = "";
conn->Close();
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2020-08-18 21:51
    已采纳

    String^ text2 = "Insert Into Sensor1(Temperature,Humidity)Values(";
    text2 += textBox4->Text + ",";
    text2 += textBox6->Text + ")";
    你Temperature,Humidity)Values中间的逗号是全角的

    点赞 打赏 评论

相关推荐 更多相似问题