#include <iostream.h>
#include <stdio.h>
#include <ICRSINT.H>
#include <string.h>
#import "D:\VC\c_ado\Debug\MSADO15.DLL" no_namespace rename("EOF", "adoEOF")
void main()
{
CoInitialize(NULL);
//AfxOleInit();
_ConnectionPtr pMyConnect=NULL;
HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection));
if(FAILED(hr))
{
cout<<"error 1!"<<endl;
return;
}
_bstr_t strConnect="Driver={sql server};server=qiany;uid=;pwd=;database=pubs"; //SQLSERVER
//_bstr_t strConnect="Provider=OraOLEDB.Oracle;Data Source=orcl;User Id=scott;Password=tiger;"; //ORACLE
//connecting to the database server now:
try{pMyConnect->Open(strConnect,"","",NULL);}
catch (_com_error &e)
{
cout<<"error 2!"<<endl;
//::MessageBox(NULL,e.Description(),"警告",MB_OK │ MB_ICONWARNING);
}
_RecordsetPtr m_pRecordset;
if(FAILED(m_pRecordset.CreateInstance( __uuidof( Recordset ))))
{
cout<<"error 3!"<<endl;
//m_pDoc->m_initialized=FALSE;
return;
}
try{
m_pRecordset->Open(_variant_t("authors"),
_variant_t((IDispatch *)pMyConnect,true), adOpenKeyset,
adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
cout<<"error 4!"<<endl;
}
//读取记录
try
{
m_pRecordset->MoveFirst();
while(m_pRecordset->adoEOF==VARIANT_FALSE)
{
//Retrieve column's value:
char *sName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("au_lname"))->Value);
//short cId=(short)(m_pRecordset->Fields->GetItem(_variant_t("au_id"))->Value);
cout<<(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("au_lname"))->Value)<<endl;
//cout<<sName<<endl;
m_pRecordset->MoveNext();
}
}
catch (_com_error &e)
{
cout<<"error 5!"<<endl;
}
//添加记录
try
{
if(!m_pRecordset->Supports(adAddNew))
{
cout<<"error 6!"<<endl;
return;
}
m_pRecordset->AddNew();
m_pRecordset->Fields->GetItem(_variant_t("au_id"))->Value=_bstr_t("998-72-8882");
m_pRecordset->Fields->GetItem(_variant_t("au_lname"))->Value=_bstr_t("赵薇2");
m_pRecordset->Fields->GetItem(_variant_t("au_fname"))->Value=_bstr_t("赵薇2");
m_pRecordset->Fields->GetItem(_variant_t("phone"))->Value=_bstr_t("赵薇2");
m_pRecordset->Update();
}//try
catch (_com_error &e)
{
cout<<"error 7!"<<endl;
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。