向数据库储存数据出现错误 大侠帮忙给看下
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString m_AnsiFileName;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ADOQuery1->Insert();
//保存系统信息
ADOQuery1->FieldByName("制造单位")->AsString ="1";
ADOQuery1->FieldByName("设备编号")->AsString = "2";
//ADOQuery1->FieldByName("日期") ->AsString = "3";
ADOQuery1->FieldByName("检验人员1")->AsString = "4";
ADOQuery1->FieldByName("检验人员2")->AsString = "5";
ADOQuery1->FieldByName("温度(℃)")->AsString = "6";
ADOQuery1->FieldByName("湿度(%)")->AsString = "7";
ADOQuery1->FieldByName("序号")->AsInteger = 8;
ADOQuery1->FieldByName("坠陀端")->AsFloat = 9;
ADOQuery1->FieldByName("线索端")->AsFloat = 10;
ADOQuery1->FieldByName("传动效率")->AsFloat = 11;
ADOQuery1->FieldByName("检测序号")->AsInteger = 12;
ADOQuery1->FieldByName("传动方向")->AsString = "上升";
ADOQuery1->FieldByName("传动比")->AsString = "1:";
//表示是第一个数据
ADOQuery1->FieldByName("两测点效率之差")->AsFloat = 0.97;
//将上一个的效率保存
//放在查询出来后写入
ADOQuery1->FieldByName("与效率平均值之差")->AsFloat = 0;
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
m_AnsiFileName = "c:\\hlz.mdb" ;
//将ADO连接上保存数据库
ADOConnection1->Close();
ADOConnection1->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+m_AnsiFileName+";Persist Security Info = false";
ADOConnection1->Open();
//将信息数据存进数据库
ADOQuery1->Active = false;
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("select * from Data");
ADOQuery1->Active = true;
//读取上次的序号值
ADOQuery1->Last();
// if(ADOQuery1->RecordCount >0)
// m_iLast = ADOQuery1->FieldByName("序号")->AsInteger;
}
//-----------------------
这个程序运行后可以正常的向数据库了存数据 这是一个实验 所以存的数都是确定的
经过修改后
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(MSComm1->PortOpen!=true)
{
MSComm1->CommPort=StrToInt(ComboBox1->Text);//选择串口号
MSComm1->Settings=
ComboBox2->Text+","+
ComboBox3->Text+","+
ComboBox4->Text+","+
ComboBox5->Text; //设置串口的属性波特率、奇偶校验、数据位和、//停止位。
MSComm1->InputMode=0;//设置传入数据的格式,0表示文本形式
MSComm1->PortOpen=true;//打开串口
Button1->Enabled=false;
Button2->Enabled=true;
Button3->Enabled=true;
Button4->Enabled=true;
Shape1->Brush->Color=clGreen;
}
m_AnsiFileName = "c:\\hlz.mdb" ;
//将ADO连接连接上保存数据库
ADOConnection2->Close();
ADOConnection2->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+m_AnsiFileName+";Persist Security Info = false";
ADOConnection2->Open();
//将信息数据存进数据库
ADOQuery2->Active = false;
ADOQuery2->SQL->Clear();
ADOQuery2->SQL->Add("select * from Data");
ADOQuery2->Active = true;
//读取上次的序号值
ADOQuery2->Last();
// if(ADOQuery1->RecordCount >0)
// m_iLast = ADOQuery1->FieldByName("序号")->AsInteger;
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(MSComm1->PortOpen!=false)
{
MSComm1->PortOpen=false;
Button1->Enabled=true;
Button2->Enabled=false;
Button3->Enabled=false;
Button4->Enabled=false;
Shape1->Brush->Color=clRed;
}
else
{
Button1->Enabled=false;
Button2->Enabled=true;
Shape1->Brush->Color=clRed;
}
}
//---------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
MSComm1->Output=StringToOleStr(Memo2->Text); //把AnsiString型转化成//Ole形式。
}
//---------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
//把Memo1中的数据保存在指定的文件中
AnsiString filename1;
SaveDialog1->Filter="Text files (*.txt)|*.txt|All files (*.*)|*.*";//文件类型过滤器
SaveDialog1->FilterIndex=2;
if(SaveDialog1->Execute())
{
filename1=SaveDialog1->FileName;
Memo1->Lines->SaveToFile(filename1);//把收到的数据保存在文件filename1中
}
}
//---------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
ADOConnection1->Open();
AnsiString sqlstr = "Insert into TABLE1(name)values('22222222')";
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sqlstr);
// ADOQuery1->Open();
ADOQuery1->ExecSQL();
}
//---------------------------------------
//---------------------------------------
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{
AnsiString str; //声明一个AnsiString类型的变量
OleVariant s; //声明一个用于接收数据的OleVariant变量。
AnsiString str1;
// 接收缓冲区中是否收到Rthreshold个字符。
if(MSComm1->CommEvent==comEvReceive)
{
if(MSComm1->InBufferCount)// 是否有字符驻留在接收缓冲区等待被取出
{
s=MSComm1->Input;//接收数据
str=s.AsType(varString); //把接收到的OleVariant变量转换成AnsiString类型
Memo1->Text=Memo1->Text+str;//把接收到的数据显示在Memo1中。
int a,b;
a=str.Pos(" ");
while(a!=0)
{
str1=str.SubString(1,a-1);
Memo1->Text=str1;
str.Delete(1,a);
a=str.Pos(" ");}
}
}
ADOQuery2->Insert();
//保存系统信息
ADOQuery2->FieldByName("制造单位")->AsString ="str1";
ADOQuery2->FieldByName("设备编号")->AsString = "2";
//ADOQuery1->FieldByName("日期")->AsString = "3";
ADOQuery2->FieldByName("检验人员1")->AsString = "4";
ADOQuery2->FieldByName("检验人员2")->AsString = "5";
ADOQuery2>-FieldByName("温度(℃)")->AsString = "6";
ADOQuery2->FieldByName("湿度(%)")->AsString = "7";
ADOQuery2->FieldByName("序号")->AsInteger = 8;
ADOQuery2->FieldByName("坠陀端")->AsFloat = 9;
ADOQuery2->FieldByName("线索端")->AsFloat = 10;
ADOQuery2->FieldByName("传动效率")->AsFloat = 11;
ADOQuery2->FieldByName("检测序号")->AsInteger = 12;
ADOQuery2->FieldByName("传动方向")->AsString = "上升";
ADOQuery2->FieldByName("传动比")->AsString = "1:";
//表示是第一个数据
ADOQuery1->FieldByName("两测点效率之差")->AsFloat = 0.97;
//将上一个的效率保存
//放在查询出来后写入
ADOQuery2->FieldByName("与效率平均值之差")->AsFloat = 0;
}
用串口助手传进的数据就无法存进数据库了 并出现如下错误
[C++ Error] Unit1.cpp(142): E2268 Call to undefined function 'FieldByName'
[C++ Error] Unit1.cpp(142): E2288 Pointer to structure required on left side of -> or ->*
哪位大神帮忙给看下
[解决办法]
问题出在这一行,很明显是你打字的时候失误了,把->写成>-了
ADOQuery2>-FieldByName("温度(℃)")->AsString = "6";
改成:
ADOQuery2->FieldByName("温度(℃)")->AsString = "6";
就可以了.
另外,这一句:
AnsiString sqlstr = "Insert into TABLE1(name)values('22222222')";执行的时候也会有问题的. 自己修改吧, 我就不提示了.