DALMyBools.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:7k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // DALMyBools.cpp : Implementation of CDALMyBools
  2. #include "stdafx.h"
  3. #include "BooksDAL.h"
  4. #include "DALMyBools.h"
  5. #include <vector>
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CDALMyBools
  8. STDMETHODIMP CDALMyBools::InterfaceSupportsErrorInfo(REFIID riid)
  9. {
  10. static const IID* arr[] = 
  11. {
  12. &IID_IDALMyBools
  13. };
  14. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  15. {
  16. if (InlineIsEqualGUID(*arr[i],riid))
  17. return S_OK;
  18. }
  19. return S_FALSE;
  20. }
  21. STDMETHODIMP CDALMyBools::GetBookByIsbn(UINT bookisbn, DTOBook **mybook)
  22. {
  23. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  24. // TODO: Add your implementation code here
  25. _ConnectionPtr conn=this->GetMyConn();
  26. *mybook=(DTOBook*)::CoTaskMemAlloc(sizeof(DTOBook));
  27. _CommandPtr sqlobj;
  28. sqlobj.CreateInstance(__uuidof(Command));
  29. sqlobj->CommandText=(_bstr_t)this->m_sqlQueryById;
  30. sqlobj->CommandType=adCmdText;
  31. sqlobj->ActiveConnection=conn;
  32. // _ParameterPtr para1=sqlobj->CreateParameter("id",adInteger,adParamInput,4);
  33. // para1->Value=_variant_t((long)bookisbn);
  34. sqlobj->Parameters->Append(GetIdPara(bookisbn,sqlobj));
  35. _RecordsetPtr data=sqlobj->Execute(NULL,NULL,adCmdText);
  36. if(!data->adEOF)
  37. {
  38. (*mybook)->BookIsnb=(UINT)(long)(data->GetCollect("bookisnb"));
  39. _variant_t vtname=data->GetCollect("bookname");
  40. (*mybook)->BookName=::SysAllocString(vtname.bstrVal);
  41. _variant_t vtauthor=data->GetCollect("bookauthor");
  42. (*mybook)->BookAuthor=::SysAllocString(vtauthor.bstrVal);
  43. }
  44. return S_OK;
  45. }
  46. STDMETHODIMP CDALMyBools::QueryAllBook(DTOBook **allBook,UINT* Numbers)
  47. {
  48. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  49. // TODO: Add your implementation code here
  50. _ConnectionPtr myconn=this->GetMyConn();
  51. _CommandPtr sqlobj;
  52. sqlobj.CreateInstance(__uuidof(Command));
  53.     
  54. sqlobj->ActiveConnection=myconn;
  55. sqlobj->CommandText=_bstr_t(this->m_sqlQueryAll);
  56. sqlobj->CommandType=adCmdText;
  57. _RecordsetPtr rs=sqlobj->Execute(NULL,NULL,adCmdText);
  58. //转到链表;
  59. std::vector<DTOBook*> vt;
  60. while(!rs->adEOF)
  61. {
  62. _variant_t isbn=rs->GetCollect("BOOKISNB");
  63. _variant_t name=rs->GetCollect("BOOKName");
  64. _variant_t author=rs->GetCollect("BOOKAuthor");
  65. DTOBook* obj1=new DTOBook;
  66. obj1->BookIsnb=(UINT)(long)isbn;
  67. obj1->BookName=::SysAllocString(name.bstrVal);
  68. obj1->BookAuthor=::SysAllocString(author.bstrVal);
  69. vt.push_back(obj1);
  70. rs->MoveNext();
  71. }
  72. //链表转变为数组;
  73. int isize=vt.size();
  74. DTOBook* obj1=(DTOBook*)(CoTaskMemAlloc(isize*sizeof(DTOBook)));
  75. for(int i=0;i<isize;i++)
  76. {
  77. DTOBook* pData=vt[i];
  78. (obj1+i)->BookIsnb=pData->BookIsnb;
  79. (obj1+i)->BookName=pData->BookName;
  80. (obj1+i)->BookAuthor=pData->BookAuthor;
  81. delete pData;
  82. }
  83. *allBook=obj1;
  84. *Numbers=isize;
  85. return S_OK;
  86. }
  87. STDMETHODIMP CDALMyBools::SaveBook(DTOBook *bookdata, UINT *pResult)
  88. {
  89. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  90. _ConnectionPtr conn=this->GetMyConn();
  91. _CommandPtr sqlobj;
  92. sqlobj.CreateInstance(__uuidof(Command));
  93. sqlobj->ActiveConnection=conn;
  94. sqlobj->CommandType=adCmdText;
  95. sqlobj->CommandText=_bstr_t(this->m_sqlSave);
  96. sqlobj->Parameters->Append(this->GetIdPara(bookdata->BookIsnb,sqlobj));
  97. sqlobj->Parameters->Append(this->GetNamePara(CString(bookdata->BookName),sqlobj));
  98. sqlobj->Parameters->Append(this->GetAuthorPara(CString(bookdata->BookAuthor),sqlobj));
  99. VARIANT vResult;
  100. vResult.vt=VT_I4;
  101. try{
  102. sqlobj->Execute(&vResult,NULL,adCmdText);
  103. }
  104. catch(_com_error ex)
  105. {
  106. int i=0;
  107. i++;
  108. }
  109. *pResult=vResult.lVal;
  110. // TODO: Add your implementation code here
  111. return S_OK;
  112. }
  113. _ParameterPtr CDALMyBools::GetNamePara(CString name,_CommandPtr sqlObj)
  114. {
  115. _ParameterPtr para1=sqlObj->CreateParameter("name",adVarChar,adParamInput,20);
  116. para1->Value=(_variant_t)_bstr_t(name);
  117. return para1;
  118. }
  119. _ParameterPtr CDALMyBools::GetAuthorPara(CString author,_CommandPtr sqlObj)
  120. {
  121. _ParameterPtr para1=sqlObj->CreateParameter("author",adVarChar,adParamInput,20);
  122. para1->Value=(_variant_t)_bstr_t(author);
  123. return para1;
  124. }
  125. STDMETHODIMP CDALMyBools::DeleteBookById(UINT id, UINT *iResult)
  126. {
  127. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  128. // TODO: Add your implementation code here
  129. _ConnectionPtr conn=this->GetMyConn();
  130. _CommandPtr sqlobj;
  131. sqlobj.CreateInstance(__uuidof(Command));
  132. sqlobj->CommandText=(_bstr_t)this->m_sqlDelete;
  133. sqlobj->CommandType=adCmdText;
  134. sqlobj->ActiveConnection=conn;
  135. sqlobj->Parameters->Append(GetIdPara(id,sqlobj));
  136. VARIANT vResult;
  137. vResult.vt=VT_I4;
  138. try{
  139. sqlobj->Execute(&vResult,NULL,adCmdText);
  140. }
  141. catch(_com_error ex)
  142. {
  143. int i=0;
  144. i=i+1;
  145. }
  146. *iResult=vResult.lVal;
  147. return S_OK;
  148. }
  149. STDMETHODIMP CDALMyBools::Update(DTOBook *Book, UINT *iResult)
  150. {
  151. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  152. _ConnectionPtr conn=this->GetMyConn();
  153. _CommandPtr sqlobj;
  154. sqlobj.CreateInstance(__uuidof(Command));
  155. sqlobj->ActiveConnection=conn;
  156. sqlobj->CommandType=adCmdText;
  157. sqlobj->CommandText=_bstr_t(this->m_sqlUpdate);
  158. sqlobj->Parameters->Append(this->GetNamePara(CString(Book->BookName),sqlobj));
  159. sqlobj->Parameters->Append(this->GetAuthorPara(CString(Book->BookAuthor),sqlobj));
  160. sqlobj->Parameters->Append(this->GetIdPara(Book->BookIsnb,sqlobj));
  161. VARIANT vResult;
  162. vResult.vt=VT_I4;
  163. try{
  164. sqlobj->Execute(&vResult,NULL,adCmdText);
  165. }
  166. catch(_com_error ex)
  167. {
  168. int i=0;
  169. i++;
  170. }
  171. *iResult=vResult.lVal;
  172. // TODO: Add your implementation code here
  173. return S_OK;
  174. }
  175. STDMETHODIMP CDALMyBools::get_UID(BSTR *pVal)
  176. {
  177. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  178. // TODO: Add your implementation code here
  179. return S_OK;
  180. }
  181. STDMETHODIMP CDALMyBools::put_UID(BSTR newVal)
  182. {
  183. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  184. // TODO: Add your implementation code here
  185. this->m_UID=CString(newVal);
  186. ::SysFreeString(newVal);
  187. return S_OK;
  188. }
  189. STDMETHODIMP CDALMyBools::get_PWD(BSTR *pVal)
  190. {
  191. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  192. // TODO: Add your implementation code here
  193. return S_OK;
  194. }
  195. STDMETHODIMP CDALMyBools::put_PWD(BSTR newVal)
  196. {
  197. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  198. // TODO: Add your implementation code here
  199. this->m_PWD=CString(newVal);
  200. ::SysFreeString(newVal);
  201. return S_OK;
  202. }
  203. STDMETHODIMP CDALMyBools::get_DataSource(BSTR *pVal)
  204. {
  205. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  206. // TODO: Add your implementation code here
  207. return S_OK;
  208. }
  209. STDMETHODIMP CDALMyBools::put_DataSource(BSTR newVal)
  210. {
  211. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  212. // TODO: Add your implementation code here
  213. this->m_DataSource=CString(newVal);
  214. ::SysFreeString(newVal);
  215. return S_OK;
  216. }
  217. _ConnectionPtr CDALMyBools::GetMyConn()
  218. {
  219. CString strFormat="Provider=OraOLEDB.Oracle.1;User Id=%s;Data Source=%s";
  220. CString str;
  221. str.Format(strFormat,this->m_UID,this->m_DataSource);
  222. _ConnectionPtr mytools;
  223. mytools.CreateInstance(__uuidof(Connection));
  224. mytools->Open(_bstr_t(str),"",_bstr_t(this->m_PWD),-1);
  225. return mytools;
  226. }
  227. _ParameterPtr CDALMyBools::GetIdPara(UINT id,_CommandPtr sqlObj)
  228. {
  229. _ParameterPtr para1=sqlObj->CreateParameter("id",adInteger,adParamInput,4);
  230. para1->Value=_variant_t((long)id);
  231. return para1;
  232. }