一. 數據庫的編程(ADO)
要用ADO連接數據的頭文件中加入 #import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
// 初始化OLE/COM庫環境 _ConnectionPtr m_pConnection;
::CoInitialize(NULL);
char buf[256],server[256],datebase[256],uid[256],pwd[256];char str[255];
GetCurrentDirectory(255,str);//獲得當前目錄
strcat(str,"file://dbinfo.ini");int/ len0=GetPrivateProfileString("dbinfo","Provider","No Text",buf,256,str);//讀取配置文件中的數據庫連接信息
int len1=GetPrivateProfileString("dbinfo","server","No Text",server,256,str);
int len2=GetPrivateProfileString("dbinfo","Database","No Text",datebase,256,str);
int len3=GetPrivateProfileString("dbinfo","uid","No Text",uid,256,str);
int len4=GetPrivateProfileString("dbinfo","pwd","No Text",pwd,256,str); try
{
// 創建Connection對象
m_pConnection.CreateInstance("ADODB.Connection");
// 設置連接字符串,必須是BSTR型或者_bstr_t類型
_bstr_t strConnect = "Provider=";
strConnect =strConnect+buf;
strConnect =strConnect+";Server=";
strConnect =strConnect+server;
strConnect =strConnect+";Database=";
strConnect =strConnect+datebase;
strConnect =strConnect+";uid=";
strConnect =strConnect+uid;
strConnect =strConnect+";pwd=";
strConnect =strConnect+pwd; m_pConnection->Open(strConnect,"","",adModeUnknown);//連接到指定的數據庫
}
// 捕捉異常
catch(_com_error e)
{
// 顯示錯誤信息
AfxMessageBox(e.Description());
}
執行無返回值(記錄集)的SQL語句,Insert Update等無返回值的語句
// Connection對象的Execute方法:(_bstr_t CommandText, // VARIANT * RecordsAffected, long Options )
// 其中CommandText是命令字串,通常是SQL命令。
// 參數RecordsAffected是操作完成後所影響的行數,
// 參數Options表示CommandText的類型:adCmdText-文本命令;adCmdTable-表名
// adCmdProc-存儲過程;adCmdUnknown-未知
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
執行有返回值(記錄集)的SQL語句
_RecordsetPtr m_pRecordset;
// 創建記錄集對象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 返回表中的記錄
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
CListCtrl m_list1;//記錄顯示在CListCtrl中
if(!m_pRecordset->adoEOF) //如果記錄不為空
{
while(!m_pRecordset->adoEOF)
{
m_list1.InsertItem(i,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID"));
m_list1.SetItemText(i,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("tintCallType"));
m_list1.SetItemText(i,2,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("tintHangUp"));
_variant_t vMaxid(m_pRecordset->GetCollect("strCallingNmb"));
if(vMaxid.vt==VT_NULL)
vMaxid="";
m_list1.SetItemText(i,3,(LPCTSTR)(_bstr_t)vMaxid);
_variant_t vMaxid0(m_pRecordset->GetCollect("strCalledNmb"));
if(vMaxid0.vt==VT_NULL)
vMaxid0="";
m_list1.SetItemText(i,4,(LPCTSTR)(_bstr_t)vMaxid0);
_variant_t vMaxid1(m_pRecordset->GetCollect("strOriginNmb"));
if(vMaxid1.vt==VT_NULL) //此字段在數據庫中為NULL值
vMaxid1=""; i++;//CListCtrl中的第i行顯示此記錄
m_pRecordset->MoveNext();//下一條記錄
}
}
m_pRecordset->Close();//記錄集讀取完畢,關閉記錄集
執行帶參數無返回值(一個或者多個記錄集)的存儲過程
_CommandPtr m_pnewCommand;
m_pnewCommand.CreateInstance("ADODB.Command"); m_pnewCommand->ActiveConnection=m_pConnection;
m_pnewCommand->CommandType=adCmdStoredProc;
m_pnewCommand->CommandText=_bstr_t("proc_deleteSMCBQueue");
unsigned char newflag = 1;
_ParameterPtr Pnewflag,PnewrecordID;
Pnewflag.CreateInstance(__uuidof(Parameter));
PnewrecordID.CreateInstance(__uuidof(Parameter));
CString newst="";
newst.Format("%d",recordID);
Pnewflag=m_pnewCommand->CreateParameter(_bstr_t("flag"),adTinyInt,adParamInput,1,(_variant_t)newflag);
m_pnewCommand->Parameters->Append(Pnewflag);
PnewrecordID=m_pnewCommand->CreateParameter(_bstr_t("recordID"),adInteger,adParamInput,4,(_variant_t)(_bstr_t)newst);
m_pnewCommand->Parameters->Append(PnewrecordID);
try
{
m_pnewCommand->Execute(NULL,NULL,adCmdStoredProc);
}
catch(...)
{
}
執行有參數有返回記錄集的存儲過程(其實就是執行有返回值(記錄集)的SQL語句)
CString sqlrecord ="exec proc_getSMCBQueue 1";
try
{
m_pRecordset = m_pConnection->Execute((_bstr_t)sqlrecord,NULL,adCmdText);
}
catch(...)
{
}
今天就記一下VC關於數據庫的編程吧,下次再記VC關於網絡的編程吧!
只是真正的用_CommandPtr m_pnewCommand執行存儲過程返回一個或者多個記錄集,再分別對各記錄集進行操作還沒有實現啊!