4,011
社区成员
发帖
与我相关
我的任务
分享
void SQLAccess()
{
CoInitialize(NULL);
HRESULT hr = S_OK;
// Define connection string variables.
_bstr_t strCnn("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Administrator\\Desktop\\test.mdb;Persist Security Info=False");
// Define ADO object pointers.
// Initialize pointers on define.
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRst = NULL;
try
{
// Open connection.
pConnection.CreateInstance(__uuidof(Connection));
pConnection->Open (strCnn, "", "", adConnectUnspecified);
// Please note that question mark, a question mark represents a parameter
_bstr_t strSQL("select * from table1;");
// Create command object.
pRst.CreateInstance(__uuidof(Recordset));
pRst->CursorLocation = adUseClient;
pRst->Open((_variant_t)strSQL, // 查询Club表中所有字段
pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
pRst->AddNew();
pRst->PutCollect(_variant_t("id"),_variant_t("test0001"));
pRst->PutCollect(_variant_t("employee_name"),_variant_t("name1"));
pRst->Update();
// Clean up objects before exit.
if (pRst)
{
if (pRst->State == adStateOpen)
pRst->Close();
pRst.Release();
}
if (pConnection)
{
if (pConnection->State == adStateOpen)
pConnection->Close();
pConnection.Release();
}
}
catch (_com_error &e)
{
// Print Com errors.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE(_T("Error\n"));
TRACE(_T("\tCode = %08lx\n"), e.Error());
TRACE(_T("\tErrorMessage = %s\n"), (LPCTSTR)e.ErrorMessage());
TRACE(_T("\tSource = %s\n"), (LPCTSTR)bstrSource);
TRACE(_T("\tDescription = %s\n"), (LPCTSTR)bstrDescription);
}
::CoUninitialize();
}