帮我看看写数据库的代码,为什么写不成功?
已经初试化COM!
if(lstrlen(chDataSource)<=0) return ;
if(lstrlen(chSqlSentence)<=0) return ;
IADORecordBinding *picRs = NULL;
_RecordsetPtr pRs("ADODB.Recordset");
_ConnectionPtr pConn("ADODB.Connection" );
CFootballTeamRs rsFootballTeam;
pConn->ConnectionString = chDataSource;
pConn->Provider = _T("Microsoft.Jet.OLEDB.4.0");
try
{
pConn->Open(pConn->ConnectionString, "", "", adModeUnknown);
pRs->QueryInterface(
__uuidof(IADORecordBinding), (LPVOID*)&picRs);
pRs->Open( (_variant_t)chSqlSentence, // 查询DemoTable表中所有字段
pConn.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
TESTHR(picRs->BindToRecordset(&rsFootballTeam));
}
catch (_com_error &e)
{
sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
AfxMessageBox(errMsg);
return;
}
try
{
pRs->MoveLast();
}
catch (_com_error &e)
{
sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
AfxMessageBox(errMsg);
return;
}
rsFootballTeam.m_Age = GetDlgItemInt(IDC_AGE);
rsFootballTeam.m_AttendNationNums = GetDlgItemInt(IDC_NATIONAL);
rsFootballTeam.m_Def = GetDlgItemInt(IDC_DEF);
rsFootballTeam.m_DoorFell = GetDlgItemInt(IDC_DOORFELL);
rsFootballTeam.m_FarShot = GetDlgItemInt(IDC_FAR_SHOT);
rsFootballTeam.m_Illness = GetDlgItemInt(IDC_ILLNESS);
// CString strName;
// TCHAR Name[MAX_PATH];
::GetDlgItemText(m_hWnd,IDC_NAME,rsFootballTeam.m_Name,MAX_PATH);
/* lstrcpy(Name, strName);
rsFootballTeam.m_Name = Name;*/
rsFootballTeam.m_Stature = GetDlgItemInt(IDC_STATURE);
try
{
picRs->AddNew(&rsFootballTeam);
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
TESTHR(picRs->Update(&rsFootballTeam));
}
catch (_com_error &e)
{
sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
AfxMessageBox(errMsg);
return;
}