- // UseAdo.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <tchar.h>
- #include <windows.h>
- #include <iostream>
- #include <icrsint.h>
- using namespace std;
- #import "c:/program files/common files/system/ado/msado15.dll" /
- no_namespace rename("EOF", "AdoEOF") rename("BOF", "AdoBOF")
- //global function
- BOOL ConnectDb(_ConnectionPtr pConn, LPCTSTR lpszDbPath);
- BOOL CloseConnect(_ConnectionPtr pConn);
- BOOL ExecuSql(_CommandPtr pCmd, LPCTSTR lpszSql, _RecordsetPtr& pRcord);
- int _tmain(int argc, _TCHAR* argv[])
- {
- _ConnectionPtr pConn;
- _RecordsetPtr pRecord;
- _CommandPtr pCmd;
- char szInput[MAX_PATH] = {0};
- ::CoInitialize(NULL);
- if ( FAILED(pConn.CreateInstance(_T("ADODB.Connection"))) )//创建Connection对象
- {
- cout << "CreateInstance(_T(ADODB.Connection))) failed! err code = " << ::GetLastError() << endl;
- return FALSE;
- }
- if ( FAILED(pRecord.CreateInstance("ADODB.Recordset")) )//创建RecordSet对象
- {
- cout << "CreateInstance(_T(ADODB.Recordset))) failed! err code = " << ::GetLastError() << endl;
- return FALSE;
- }
- if ( FAILED(pCmd.CreateInstance("ADODB.Command")) )//创建Command对象
- {
- cout << "CreateInstance(_T(ADODB.Command))) failed! err code = " << ::GetLastError() << endl;
- return FALSE;
- }
- //连接数据库
- if (!ConnectDb(pConn, "./testado.mdb"))
- {
- return FALSE;
- }
- pCmd->ActiveConnection = pConn;
- pCmd->CommandType = adCmdText;
- while (TRUE)
- {
- cout << "please input sql:" << endl;
- cin.getline(szInput, MAX_PATH);
- //全部转换成小写
- ::strlwr(szInput);
- if ( (::strncmp(szInput, "quit", sizeof(szInput)) == 0) || (::strncmp(szInput, "exit", sizeof(szInput)) == 0) )
- {
- break;
- }
- if (!ExecuSql(pCmd, szInput, pRecord))
- {
- cout << "execute sql failed! please check you input!" << endl;
- continue;
- }
- if (::strncmp(szInput, "select", strlen("select")) == 0)
- {
- while (!pRecord->GetAdoEOF())
- {
- _variant_t vt;
- //get num
- vt = pRecord->GetCollect("snum");
- if (vt.vt != VT_NULL)
- {
- cout << "num = " << vt.intVal << "/t";
- }
- //get name
- vt = pRecord->GetCollect("sname");
- if (vt.vt != VT_NULL)
- {
- cout << "name = " << _com_util::ConvertBSTRToString(vt.bstrVal) << "/t";
- }
- //get sex
- vt = pRecord->GetCollect("ssex");
- if (vt.vt != VT_NULL)
- {
- cout << "sex = " << _com_util::ConvertBSTRToString(vt.bstrVal) << "/t";
- }
- //get address
- vt = pRecord->GetCollect("saddr");
- if (vt.vt != VT_NULL)
- {
- cout << "address = " << _com_util::ConvertBSTRToString(vt.bstrVal) << endl;
- }
- pRecord->MoveNext();
- }
- }
- }
- ::CoUninitialize();
- return 0;
- }
- BOOL ConnectDb(_ConnectionPtr pConn, LPCTSTR lpszDbPath)
- {
- HRESULT hRet = S_FALSE;
- TCHAR szOpen[MAX_PATH] = {0};
- try
- {
- ::_sntprintf(szOpen, sizeof(szOpen), _T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = %s"), lpszDbPath);
- if ( FAILED(pConn->Open(szOpen, _T(""), _T(""), adModeUnknown)) ) //连接数据库
- {
- cout << "open data base failed! open str = " << szOpen << endl;
- return FALSE;
- }
- }
- catch (_com_error ex)
- {
- cout << "connect database exception! err info: " << ex.ErrorMessage() << endl;
- return FALSE;
- }
- return TRUE;
- }
- BOOL CloseConnect(_ConnectionPtr pConn)
- {
- if (NULL == pConn)
- {
- return FALSE;
- }
- try
- {
- if (adStateClosed != pConn->State)
- {
- if ( FAILED(pConn->Close()) )
- {
- cout << "close database connection failed!" << endl;
- return FALSE;
- }
- }
- else
- {
- cout << "the database didn't open!" << endl;
- return FALSE;
- }
- }
- catch (_com_error exp)
- {
- cout << "close database exception!/nsource: " << (LPCTSTR)exp.Source() << " description: " << (LPCTSTR)exp.Description() << endl;
- return FALSE;
- }
- return TRUE;
- }
- BOOL ExecuSql(_CommandPtr pCmd, LPCTSTR lpszSql, _RecordsetPtr& pRecord)
- {
- try
- {
- pCmd->CommandText = lpszSql;
- pCmd->Parameters->Refresh();
- pRecord = pCmd->Execute(NULL, NULL, adCmdText);
- }
- catch (_com_error exp)
- {
- cout << "execute sql failed!" << endl;
- return FALSE;
- }
- return TRUE;
- }
ADO的简单使用(vc源代码)
最新推荐文章于 2020-07-05 22:34:38 发布