我的数据库是MYSQL ,表T1(id int(4) not null auto_increment primary key ,name varchar(128) not null default '')type=innodb ,我现在要往T1中添加一条记录,如下:
_RecordsetPtr m_pRecord;//
我只写部分代码
//////////////////////////////////////////////////////////////////
//_RecordsetPtr m_pRecord;//已打开
_variant_t vid,vname;
CString str;
try{
if(m_pRecord->State)
{
vid=(long)param->id;
str.Format("%s","ddddd");
vname=str;
m_pRecord->AddNew();
m_pRecord->PutCollect("id",vid);
m_pRecord->PutCollect("name",vname);
m_pRecord->Update();
}
}catch (_com_error &e) {
AfxMessageBox(e.ErrorMessage());
}
用debug调试,在执行到m_pRecord->PutCollect("name",vname);语句是调到了异常处理中,输出“idispatch error #3105”的错误,请问大虾如何解决?
_RecordsetPtr m_pRecord;//
我只写部分代码
//////////////////////////////////////////////////////////////////
//_RecordsetPtr m_pRecord;//已打开
_variant_t vid,vname;
CString str;
try{
if(m_pRecord->State)
{
vid=(long)param->id;
str.Format("%s","ddddd");
vname=str;
m_pRecord->AddNew();
m_pRecord->PutCollect("id",vid);
m_pRecord->PutCollect("name",vname);
m_pRecord->Update();
}
}catch (_com_error &e) {
AfxMessageBox(e.ErrorMessage());
}
用debug调试,在执行到m_pRecord->PutCollect("name",vname);语句是调到了异常处理中,输出“idispatch error #3105”的错误,请问大虾如何解决?
改成
AfxMessageBox(e.Description());
这样的话,你就知道原因了..
上面那个只提供错误代码..搞不清楚
上面的问题已解决,只是把str.Format("%s","ddddd");
vname=str;改为 vname=str.GetBuffer();即可,但是奇怪的事情又发生了,就是我添加的一条记录(如 id=2,name=eeee),在数据库中此条记录是(2,eeee),但我在添加代码中又重新显示所有记录,则新的显示(0,eeee);
代码如下:
void addRecord()
{
......
_variant_t vid,vname;
CString str;
try{
if(m_pRecord->State)
{
vid=(long)param->id;
str.Format("%s","ddddd");
vname=str.GetBuffer();
m_pRecord->AddNew();
m_pRecord->PutCollect("id",vid);
m_pRecord->PutCollect("name",vname);
m_pRecord->Update();
}
}catch (_com_error &e) {
AfxMessageBox(e.ErrorMessage());
}
//////////////重新显示记录///////////////////
// CListCtrl m_list;
m_pRecord->MoveFirst();
int i=0;
CString str;
_variant_t vtid,vtname;
while (! m_pRecord->adoEOF) {
vtid= m_pRecord->GetCollect("id");
m_list.InsertItem(i,(_bstr_t)vtid);
vtname= m_pRecord->GetCollect("name");
str=(LPCTSTR)(_bstr_t)vtname;
m_RecordList.SetItemText(i,1,str);
m_pRecord->MoveNext();
i++;
}
}
///////////在测试时发现只有新增加的记录显示不对,其他的都对!!!
测试结果如下
id name //原来的
1 wwww //原来的
2 dddd //原来的
0 sssss //新增的
0 xxxxxx //新增的
在数据库中为
id name
1 wwww //原来的
2 dddd //原来的
3 sssss //新增的
4 xxxxxx //新增的
/////////////////////////////////////
出现上面的结果,我不知道为什么id变为0,而后面的字段显示是正确的?同时请教高手如何更新记录,而后显示记录?
vid.vt = VT_LONG;
str.Format("%s","ddddd");
vname=str.GetBuffer();
vname.vt = VT_BSTR;
m_pRecord->AddNew();
m_pRecord->PutCollect("id",vid);
m_pRecord->PutCollect("name",vname);
m_pRecord->Update();
在编译过程中说VT_LONG未声明?请问VT_LONG的头文件是什么