본문 바로가기

CS/Database

ADO 사용 예제



 

ADOX::_CatalogPtr m_pCatalog;

ADOX::_TablePtr m_pTable;

ADOX::_ColumnPtr m_pCol1;

ADOX::_ColumnPtr m_pCol2;

ADODB::_ConnectionPtr m_pConn;

ADODB::_RecordsetPtr m_pRs;

//ADOX - Create Data Source

_bstr_t strcnn(("Provider='Microsoft.JET.OLEDB.4.0';Data source = C:\\test.mdb"));

m_pCatalog.CreateInstance(__uuidof (ADOX::Catalog));

m_pCatalog->Create(strcnn);

 

m_pTable.CreateInstance(__uuidof(ADOX::Table));

m_pTable->PutName("MyTable");

 

m_pCol1.CreateInstance(__uuidof(ADOX::Column));

m_pCol1->Name = "Column 1";

m_pCol1->Type = ADOX::adVarWChar;

m_pCol1->DefinedSize = 24;

m_pCol1->Attributes = ADOX::adColNullable;

m_pTable->Columns->Append(m_pCol1->Name, ADOX::adVarWChar, 24);

 

m_pCol2.CreateInstance(__uuidof(ADOX::Column));

m_pCol2->Name = "Column 2";

m_pCol2->Type = ADOX::adVarWChar;

m_pCol2->DefinedSize = 24;

m_pCol2->Attributes = ADOX::adColNullable;

m_pTable->Columns->Append(m_pCol2->Name, ADOX::adVarWChar, 24);

 

m_pCatalog->Tables->Append(_variant_t((IDispatch *)m_pTable));

 

 

//ADO - Data Access

m_pConn.CreateInstance (__uuidof(ADODB::Connection));

m_pConn->Open(strcnn,_bstr_t(""),_bstr_t(""),ADODB::adOpenUnspecified);

m_pRs.CreateInstance(__uuidof(ADODB::Recordset));

m_pRs->Open("MyTable", _variant_t((IDispatch *)m_pConn,true),ADODB::adOpenKeyset, ADODB::adLockOptimistic, ADODB::adCmdTable);

 

// Define a SafeArray that contains field names.

SAFEARRAY * psaFields;

SAFEARRAYBOUND aDimFields[1];

aDimFields[0].lLbound = 0;

aDimFields[0].cElements = 1;

psaFields = SafeArrayCreate(VT_VARIANT, 1, aDimFields);

 

// Create a SafeArray for values.

SAFEARRAY * psaValues;

SAFEARRAYBOUND aDimValues[1];

aDimValues[0].lLbound = 0;

aDimValues[0].cElements = 1;

psaValues = SafeArrayCreate(VT_VARIANT, 1, aDimValues);

long ix[1];

_variant_t var;

 

//Insert Data

ix[0] = 0;

var = "Column 1";

SafeArrayPutElement(psaFields, ix, (void*) (VARIANT *) (&var));

ix[0] = 0;

var = "test data row 1 col 1 Only";

SafeArrayPutElement(psaValues, ix, (void*)(VARIANT *) &var);

_variant_t vtFields, vtValues;

vtFields.vt = VT_ARRAY | VT_VARIANT;

vtValues.vt = VT_ARRAY | VT_VARIANT;

vtFields.parray = psaFields;

vtValues.parray = psaValues;

 

m_pRs->AddNew(vtFields, vtValues);  //!! Fails Here !!

 

m_pRs->Update();

m_pRs->Close();

m_pConn->Close();

'CS > Database' 카테고리의 다른 글

Oracle Client 설치방법  (0) 2010.05.28
MSSQL 주요 함수 정리  (0) 2009.06.04
MySQL 주요 문법 정리  (0) 2009.06.03
ADO를 이용한 MDB 생성 / 연동 / 압축  (0) 2009.04.28