代码如下:运行提示主键不能为空,ID就是主键,它是自增长的,不能象下面这么写吗? sqlstr.Format("insert into News ([ID],[Title],[SavePathFileName],[FileName],[Hits],[OnTop], [Elite],[ClassID],[ChnlID]) values (1, '%s', '%s', '%s', %ld, %ld, %ld, %ld, %ld)", temp.strFilename, temp.strFilepath, temp.strFilename, i, i, i, i, i);
try
{
m_pConnection->Execute(sqlstr.GetBuffer(0), &RecordsAffected, adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return ;
}
try
{
m_pConnection->Execute(sqlstr.GetBuffer(0), &RecordsAffected, adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return ;
}
贴上自己的例子:
_ConnectionPtr m_pConnection;
HRESULT hr;
class _bstr_t me;
try {
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\\bh.mdb","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage; errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息
}
_bstr_t strQuery;//
_variant_t RecordsAffected;
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
CString xuhao="1",idchuanxing="2",chuchang="3";
strQuery="INSERT INTO chuchang(序号,ID卡串行序列号,出厂编号) VALUES ('"+xuhao+"','"+idchuanxing+"','"+chuchang+"')";
m_pRecordset=m_pConnection->Execute(strQuery,&RecordsAffected,adCmdText);
其中m_pConnection->Open中DataSource=c:\\bh.mdb,c:\\bh.mdb是access数据库的文件位置,chuchang表里面的表项,序号是另外增加出来的,不作为主键,有另外作为主键的表现,但是这个表现不做插入