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

ActiveX/DCOM/ATL

开发平台:

Visual C++

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