作者:BY
www.gudianxiaoshuo.com
本文由 代碼助手軟件 整理發(fā)布 內(nèi)容與本軟件無關(guān)
更愜意的讀、更舒心的寫、更輕松的發(fā)布
秀色書文軟件 可聽、可讀、可寫、可知識發(fā)掘、可標注,再加上史上最強大的純文本配色功能, 瞬間使您的書文秀色起來。
insert into 語句總是出現(xiàn) 未指定毛病
CString strCmd;
strCmd.Format(_T("insert into Keshi values(%d,"%s")"),
nKeshi,
keshiName
);
緣由,用ACCESS 生成的數(shù)據(jù)庫, 制作表時 有1個默許的字段 保存自動ID的,將其刪除掉便可
//沒有此學生的話 則插入學生記錄
strCmd.Format(
_T("insert into 班級表%d(學號,姓名,地址,聯(lián)系方式,備注) values(%d,"%s","%s","%s","%s")"),
m_pParentStudentInfo->m_nClassID,
nXuehao,
nameStr,
addStr,
lianxiStr,
beizhuStr);
spCmd->CommandText=(LPCTSTR)strCmd;
spCmd->Execute(NULL,NULL,adCmdText);
_CommandPtr spCmd;
spCmd.CreateInstance(__uuidof(Command));
spCmd->ActiveConnection=m_pConnection;
//肯定是不是已有此學生
CString strCmd;
strCmd.Format(
_T("select count(*) from 班級表%d where 學號=%d"),
m_pParentStudentInfo->m_nClassID,
nXuehao);
_RecordsetPtr pRecordSet;
long count=0;
spCmd->CommandText=(LPCTSTR)strCmd;
pRecordSet=spCmd->Execute(NULL,NULL,adCmdText);
count=(long)pRecordSet->GetCollect ((long)0);
if (count>0)
{
AfxMessageBox (L"已存在此學號,請輸入下1個學生的學號");
return FALSE;
}
//打開此學生的記錄集
// +----------------------------------------------
pRecordSet.CreateInstance(__uuidof(Recordset));
strCmd.Format(
_T("SELECT * FROM 班級表%d order by 學號 "),
m_pParentStudentInfo->m_nClassID);
pRecordSet->Open((_variant_t)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
pRecordSet->AddNew();
pRecordSet->PutCollect(_T("學號"),(long)nXuehao);
pRecordSet->PutCollect(_T("姓名"),(LPCTSTR)nameStr);
pRecordSet->PutCollect(_T("性別"),(LPCTSTR)sexStr);
pRecordSet->PutCollect(_T("地址"),(LPCTSTR)addStr);
pRecordSet->PutCollect(_T("聯(lián)系方式"),(LPCTSTR)lianxiStr);
pRecordSet->PutCollect(_T("備注"),(LPCTSTR)beizhuStr);
pRecordSet->Update();
批量插入記錄 到所有受影響的表中
//為指定學期的課堂成績表 增加學生
BOOL CStuLianxi::AddXueqiStudent(const int nXueqi,const int nXuehao,const CString &nameStr)
{
_CommandPtr spCmd;
spCmd.CreateInstance(__uuidof(Command));
spCmd->ActiveConnection=m_pConnection;
CString strCmd;
_RecordsetPtr pRecordSet;
long count=0;
try{
strCmd.Format(
_T("select count(*) from 課堂成績表%d where 學號=%d"),
nXueqi,
nXuehao);
spCmd->CommandText=(LPCTSTR)strCmd;
pRecordSet=spCmd->Execute(NULL,NULL,adCmdText);
count=(long)pRecordSet->GetCollect ((long)0);
if (count>0)
{ //以存在此學號
return TRUE;
}
//將此學生插入課堂成績表中
strCmd.Format(
_T("insert into 課堂成績表%d(學號,姓名,優(yōu)秀,良,1般,曠課,主動加分) values(%d,"%s",0,0,0,0,0)"),
nXueqi,
nXuehao,
nameStr);
spCmd->CommandText=(LPCTSTR)strCmd;
spCmd->Execute(NULL,NULL,adCmdText);
}
catch(_com_error &e)
{
AfxMessageBox(e.Error ());
return FALSE;
}
return TRUE;
}
//為所有受影響的學生表中 添加學生
BOOL CStuLianxi::AddAllaffectedXueqiStudent(const int nXuehao,const CString &nameStr)
{
try{
CString strCmd;
_RecordsetPtr pRecordSet;
pRecordSet.CreateInstance(__uuidof(Recordset));
int nXueqiID=0;
//更新本班所有相干的課堂成績表
//查找本班所有學期
strCmd.Format(
_T("SELECT * FROM ClassInfo where 班級ID=%d"),
m_pParentStudentInfo->m_nClassID);
pRecordSet->Open ((LPCTSTR)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
if (!pRecordSet->adoBOF)
{
pRecordSet->MoveFirst ();
}else
return FALSE;
//更新本班所有學期
_variant_t varValue;
while (!pRecordSet->adoEOF)
{
varValue=pRecordSet->GetCollect (_T("ID"));
if (varValue.vt!=VT_NULL)
{
nXueqiID=_ttoi((LPCTSTR)_bstr_t(varValue));
AddXueqiStudent(nXueqiID,nXuehao,nameStr);
}else{
pRecordSet->MoveNext ();
continue;
}
pRecordSet->MoveNext ();
}
}
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
}
return TRUE;
}