m0_46820994
2020-08-20 17:06
采纳率: 57.1%
浏览 72

利用timer控件定时写数据进入access数据库,编译成功,但是运行后报错:“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,"Sensor1");
dataGridView1->DataSource = ds->Tables[0]->DefaultView;
os->Close();
}
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{
String^ text1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Office2019\MyProject\sensor.accdb;";
OleDbConnection^ conn = gcnew OleDbConnection(text1);
String^ txtCommand = "select Num,Time,Temperature,Humidity from Sensor1";
OleDbDataAdapter^ da = gcnew OleDbDataAdapter(txtCommand, conn);
DataSet^ ds = gcnew DataSet("ds");
da->Fill(ds, "Sensor1");
dataGridView1->DataSource = ds;
dataGridView1->DataMember = "Sensor1";
}
private: System::Void timer3_Tick(System::Object^ sender, System::EventArgs^ e) {
while (tbxHumi->Text != "" || tbxTemp->Text != "")
{
String^ text1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Office2019\MyProject\sensor.accdb;";
String^ text2 = "Insert Into Sensor1(Temperature,Humidity)Values('";
text2 += tbxTemp->Text + "','";
text2 += tbxHumi->Text + "')";
OleDbConnection^ conn = gcnew OleDbConnection(text1);
conn->Open();
OleDbCommand^ da = gcnew OleDbCommand();
da->CommandText = text2;
da->Connection = conn;
da->ExecuteNonQuery();
tbxTemp->Clear();
tbxHumi->Clear();
conn->Close();
}
}

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

1条回答 默认 最新

  • blownewbee 2020-08-20 21:37
    已采纳

    你的字段的数据类型,和传入的类型不匹配。修改下你的查询。字符串要引号,非字符串不要引号。

    问题解决的话,请及时采纳,包括之前的提问,这样才能继续详细回答你。

    点赞 打赏 评论

相关推荐 更多相似问题