DALMyBools.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:7k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- // DALMyBools.cpp : Implementation of CDALMyBools
- #include "stdafx.h"
- #include "BooksDAL.h"
- #include "DALMyBools.h"
- #include <vector>
- /////////////////////////////////////////////////////////////////////////////
- // CDALMyBools
- STDMETHODIMP CDALMyBools::InterfaceSupportsErrorInfo(REFIID riid)
- {
- static const IID* arr[] =
- {
- &IID_IDALMyBools
- };
- for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
- {
- if (InlineIsEqualGUID(*arr[i],riid))
- return S_OK;
- }
- return S_FALSE;
- }
- STDMETHODIMP CDALMyBools::GetBookByIsbn(UINT bookisbn, DTOBook **mybook)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- _ConnectionPtr conn=this->GetMyConn();
- *mybook=(DTOBook*)::CoTaskMemAlloc(sizeof(DTOBook));
- _CommandPtr sqlobj;
- sqlobj.CreateInstance(__uuidof(Command));
- sqlobj->CommandText=(_bstr_t)this->m_sqlQueryById;
- sqlobj->CommandType=adCmdText;
- sqlobj->ActiveConnection=conn;
- // _ParameterPtr para1=sqlobj->CreateParameter("id",adInteger,adParamInput,4);
- // para1->Value=_variant_t((long)bookisbn);
- sqlobj->Parameters->Append(GetIdPara(bookisbn,sqlobj));
- _RecordsetPtr data=sqlobj->Execute(NULL,NULL,adCmdText);
- if(!data->adEOF)
- {
- (*mybook)->BookIsnb=(UINT)(long)(data->GetCollect("bookisnb"));
- _variant_t vtname=data->GetCollect("bookname");
- (*mybook)->BookName=::SysAllocString(vtname.bstrVal);
- _variant_t vtauthor=data->GetCollect("bookauthor");
- (*mybook)->BookAuthor=::SysAllocString(vtauthor.bstrVal);
- }
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::QueryAllBook(DTOBook **allBook,UINT* Numbers)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- _ConnectionPtr myconn=this->GetMyConn();
- _CommandPtr sqlobj;
- sqlobj.CreateInstance(__uuidof(Command));
- sqlobj->ActiveConnection=myconn;
- sqlobj->CommandText=_bstr_t(this->m_sqlQueryAll);
- sqlobj->CommandType=adCmdText;
- _RecordsetPtr rs=sqlobj->Execute(NULL,NULL,adCmdText);
- //转到链表;
- std::vector<DTOBook*> vt;
- while(!rs->adEOF)
- {
- _variant_t isbn=rs->GetCollect("bookisnb");
- _variant_t name=rs->GetCollect("bookname");
- _variant_t author=rs->GetCollect("BOOKAuthor");
- DTOBook* obj1=new DTOBook;
- obj1->BookIsnb=(UINT)(long)isbn;
- obj1->BookName=::SysAllocString(name.bstrVal);
- obj1->BookAuthor=::SysAllocString(author.bstrVal);
- vt.push_back(obj1);
- rs->MoveNext();
- }
- //链表转变为数组;
- int isize=vt.size();
- DTOBook* obj1=(DTOBook*)(CoTaskMemAlloc(isize*sizeof(DTOBook)));
- for(int i=0;i<isize;i++)
- {
- DTOBook* pData=vt[i];
- (obj1+i)->BookIsnb=pData->BookIsnb;
- (obj1+i)->BookName=pData->BookName;
- (obj1+i)->BookAuthor=pData->BookAuthor;
- delete pData;
- }
- *allBook=obj1;
- *Numbers=isize;
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::SaveBook(DTOBook *bookdata, UINT *pResult)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- _ConnectionPtr conn=this->GetMyConn();
- _CommandPtr sqlobj;
- sqlobj.CreateInstance(__uuidof(Command));
- sqlobj->ActiveConnection=conn;
- sqlobj->CommandType=adCmdText;
- sqlobj->CommandText=_bstr_t(this->m_sqlSave);
- sqlobj->Parameters->Append(this->GetIdPara(bookdata->BookIsnb,sqlobj));
- sqlobj->Parameters->Append(this->GetNamePara(CString(bookdata->BookName),sqlobj));
- sqlobj->Parameters->Append(this->GetAuthorPara(CString(bookdata->BookAuthor),sqlobj));
- VARIANT vResult;
- vResult.vt=VT_I4;
- try{
- sqlobj->Execute(&vResult,NULL,adCmdText);
- }
- catch(_com_error ex)
- {
- int i=0;
- i++;
- }
- *pResult=vResult.lVal;
- // TODO: Add your implementation code here
- return S_OK;
- }
- _ParameterPtr CDALMyBools::GetNamePara(CString name,_CommandPtr sqlObj)
- {
- _ParameterPtr para1=sqlObj->CreateParameter("name",adVarChar,adParamInput,20);
- para1->Value=(_variant_t)_bstr_t(name);
- return para1;
- }
- _ParameterPtr CDALMyBools::GetAuthorPara(CString author,_CommandPtr sqlObj)
- {
- _ParameterPtr para1=sqlObj->CreateParameter("author",adVarChar,adParamInput,20);
- para1->Value=(_variant_t)_bstr_t(author);
- return para1;
- }
- STDMETHODIMP CDALMyBools::DeleteBookById(UINT id, UINT *iResult)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- _ConnectionPtr conn=this->GetMyConn();
- _CommandPtr sqlobj;
- sqlobj.CreateInstance(__uuidof(Command));
- sqlobj->CommandText=(_bstr_t)this->m_sqlDelete;
- sqlobj->CommandType=adCmdText;
- sqlobj->ActiveConnection=conn;
- sqlobj->Parameters->Append(GetIdPara(id,sqlobj));
- VARIANT vResult;
- vResult.vt=VT_I4;
- try{
- sqlobj->Execute(&vResult,NULL,adCmdText);
- }
- catch(_com_error ex)
- {
- int i=0;
- i=i+1;
- }
- *iResult=vResult.lVal;
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::Update(DTOBook *Book, UINT *iResult)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- _ConnectionPtr conn=this->GetMyConn();
- _CommandPtr sqlobj;
- sqlobj.CreateInstance(__uuidof(Command));
- sqlobj->ActiveConnection=conn;
- sqlobj->CommandType=adCmdText;
- sqlobj->CommandText=_bstr_t(this->m_sqlUpdate);
- sqlobj->Parameters->Append(this->GetNamePara(CString(Book->BookName),sqlobj));
- sqlobj->Parameters->Append(this->GetAuthorPara(CString(Book->BookAuthor),sqlobj));
- sqlobj->Parameters->Append(this->GetIdPara(Book->BookIsnb,sqlobj));
- VARIANT vResult;
- vResult.vt=VT_I4;
- try{
- sqlobj->Execute(&vResult,NULL,adCmdText);
- }
- catch(_com_error ex)
- {
- int i=0;
- i++;
- }
- *iResult=vResult.lVal;
- // TODO: Add your implementation code here
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::get_UID(BSTR *pVal)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::put_UID(BSTR newVal)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- this->m_UID=CString(newVal);
- ::SysFreeString(newVal);
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::get_PWD(BSTR *pVal)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::put_PWD(BSTR newVal)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- this->m_PWD=CString(newVal);
- ::SysFreeString(newVal);
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::get_DataSource(BSTR *pVal)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- return S_OK;
- }
- STDMETHODIMP CDALMyBools::put_DataSource(BSTR newVal)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- // TODO: Add your implementation code here
- this->m_DataSource=CString(newVal);
- ::SysFreeString(newVal);
- return S_OK;
- }
- _ConnectionPtr CDALMyBools::GetMyConn()
- {
- CString strFormat="Provider=OraOLEDB.Oracle.1;User Id=%s;Data Source=%s";
- CString str;
- str.Format(strFormat,this->m_UID,this->m_DataSource);
- _ConnectionPtr mytools;
- mytools.CreateInstance(__uuidof(Connection));
- mytools->Open(_bstr_t(str),"",_bstr_t(this->m_PWD),-1);
- return mytools;
- }
- _ParameterPtr CDALMyBools::GetIdPara(UINT id,_CommandPtr sqlObj)
- {
- _ParameterPtr para1=sqlObj->CreateParameter("id",adInteger,adParamInput,4);
- para1->Value=_variant_t((long)id);
- return para1;
- }