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. /////////////////////////////////////////////////////////////////////////////
  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;//"select * from ocybooks where bookisnb=?";
  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. _ConnectionPtr conn=this->GetMyConn();
  49. _CommandPtr sqlobj;
  50.  //   _CommandPtr mysql1; //命令智能指针
  51. sqlobj.CreateInstance(__uuidof(Command));//生成实例
  52. sqlobj->ActiveConnection=conn;
  53. sqlobj->CommandText=_bstr_t(this->m_sqlQueryAll);//指定命令文本
  54. sqlobj->CommandType=adCmdText;//指定命令类型
  55. _RecordsetPtr mydata=sqlobj->Execute(NULL,NULL,adCmdText);//数据操作
  56. while(!mydata->adEOF)//列表指针
  57. {
  58. _variant_t myid=mydata->GetCollect("BOOKISNB");//获取数据表对应属性名的数据
  59. _variant_t myname=mydata->GetCollect("BOOKNAME");//获取数据表对应属性名的数据
  60. _variant_t myauthor=mydata->GetCollect("BOOKAuthor");//获取数据表对应属性名的数据
  61. CString strName((LPCTSTR)(_bstr_t)myname);//(_bstr_t)为带空字节头的UNicode码
  62. CString strAuthor((LPCTSTR)(_bstr_t)myauthor);//(LPCTSTR)为不带空字节头的UNicode码
  63. CString str;
  64. str.Format("%d;%s;%s",(long)myid,strName,strAuthor);
  65. MessageBox(NULL,str,"title",0);
  66. mydata->MoveNext();
  67. }
  68. mydata->Close();
  69. conn->Close();
  70. /**/
  71. // TODO: Add your implementation code here
  72. return S_OK;
  73. }
  74. STDMETHODIMP CDALMyBools::SaveBook(DTOBook *bookdata, UINT *pResult)
  75. {
  76. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  77. _ConnectionPtr conn=this->GetMyConn();
  78. _CommandPtr sqlobj;
  79. sqlobj.CreateInstance(__uuidof(Command));
  80. sqlobj->ActiveConnection=conn;
  81. sqlobj->CommandType=adCmdText;
  82. sqlobj->CommandText=_bstr_t(this->m_sqlSave);
  83. sqlobj->Parameters->Append(this->GetIdPara(bookdata->BookIsnb,sqlobj));
  84. sqlobj->Parameters->Append(this->GetNamePara(CString(bookdata->BookName),sqlobj));
  85. sqlobj->Parameters->Append(this->GetAuthorPara(CString(bookdata->BookAuthor),sqlobj));
  86. VARIANT vResult;
  87. vResult.vt=VT_I4;
  88. try{
  89. sqlobj->Execute(&vResult,NULL,adCmdText);
  90. }
  91. catch(_com_error ex)
  92. {
  93. int i=0;
  94. i++;
  95. }
  96. *pResult=vResult.lVal;
  97. // TODO: Add your implementation code here
  98. return S_OK;
  99. }
  100. _ParameterPtr CDALMyBools::GetNamePara(CString name,_CommandPtr sqlObj)
  101. {
  102. _ParameterPtr para1=sqlObj->CreateParameter("name",adVarChar,adParamInput,20);
  103. para1->Value=(_variant_t)_bstr_t(name);
  104. return para1;
  105. }
  106. _ParameterPtr CDALMyBools::GetAuthorPara(CString author,_CommandPtr sqlObj)
  107. {
  108. _ParameterPtr para1=sqlObj->CreateParameter("author",adVarChar,adParamInput,20);
  109. para1->Value=(_variant_t)_bstr_t(author);
  110. return para1;
  111. }
  112. STDMETHODIMP CDALMyBools::DeleteBookById(UINT id, UINT *iResult)
  113. {
  114. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  115. // TODO: Add your implementation code here
  116. _ConnectionPtr conn=this->GetMyConn();
  117. _CommandPtr sqlobj;
  118. sqlobj.CreateInstance(__uuidof(Command));
  119. sqlobj->CommandText=(_bstr_t)this->m_sqlDelete;
  120. sqlobj->CommandType=adCmdText;
  121. sqlobj->ActiveConnection=conn;
  122. sqlobj->Parameters->Append(GetIdPara(id,sqlobj));
  123. VARIANT vResult;
  124. vResult.vt=VT_I4;
  125. try{
  126. sqlobj->Execute(&vResult,NULL,adCmdText);
  127. }
  128. catch(_com_error ex)
  129. {
  130. int i=0;
  131. i=i+1;
  132. }
  133. *iResult=vResult.lVal;
  134. return S_OK;
  135. }
  136. STDMETHODIMP CDALMyBools::Update(DTOBook *Book, UINT *iResult)
  137. {
  138. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  139. _ConnectionPtr conn=this->GetMyConn();
  140. _CommandPtr sqlobj;
  141. sqlobj.CreateInstance(__uuidof(Command));
  142. sqlobj->ActiveConnection=conn;
  143. sqlobj->CommandType=adCmdText;
  144. sqlobj->CommandText=_bstr_t(this->m_sqlUpdate);
  145. sqlobj->Parameters->Append(this->GetNamePara(CString(Book->BookName),sqlobj));
  146. sqlobj->Parameters->Append(this->GetAuthorPara(CString(Book->BookAuthor),sqlobj));
  147. sqlobj->Parameters->Append(this->GetIdPara(Book->BookIsnb,sqlobj));
  148. VARIANT vResult;
  149. vResult.vt=VT_I4;
  150. try{
  151. sqlobj->Execute(&vResult,NULL,adCmdText);
  152. }
  153. catch(_com_error ex)
  154. {
  155. int i=0;
  156. i++;
  157. }
  158. *iResult=vResult.lVal;
  159. // TODO: Add your implementation code here
  160. return S_OK;
  161. }
  162. STDMETHODIMP CDALMyBools::get_UID(BSTR *pVal)
  163. {
  164. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  165. // TODO: Add your implementation code here
  166. return S_OK;
  167. }
  168. STDMETHODIMP CDALMyBools::put_UID(BSTR newVal)
  169. {
  170. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  171. // TODO: Add your implementation code here
  172. this->m_UID=CString(newVal);
  173. ::SysFreeString(newVal);
  174. return S_OK;
  175. }
  176. STDMETHODIMP CDALMyBools::get_PWD(BSTR *pVal)
  177. {
  178. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  179. // TODO: Add your implementation code here
  180. return S_OK;
  181. }
  182. STDMETHODIMP CDALMyBools::put_PWD(BSTR newVal)
  183. {
  184. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  185. // TODO: Add your implementation code here
  186. this->m_PWD=CString(newVal);
  187. ::SysFreeString(newVal);
  188. return S_OK;
  189. }
  190. STDMETHODIMP CDALMyBools::get_DataSource(BSTR *pVal)
  191. {
  192. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  193. // TODO: Add your implementation code here
  194. return S_OK;
  195. }
  196. STDMETHODIMP CDALMyBools::put_DataSource(BSTR newVal)
  197. {
  198. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  199. // TODO: Add your implementation code here
  200. this->m_DataSource=CString(newVal);
  201. ::SysFreeString(newVal);
  202. return S_OK;
  203. }
  204. _ConnectionPtr CDALMyBools::GetMyConn()
  205. {
  206. CString strFormat="Provider=OraOLEDB.Oracle.1;User Id=%s;Data Source=%s";
  207. CString str;
  208. str.Format(strFormat,this->m_UID,this->m_DataSource);
  209. _ConnectionPtr mytools;
  210. mytools.CreateInstance(__uuidof(Connection));
  211. mytools->Open(_bstr_t(str),"",_bstr_t(this->m_PWD),-1);
  212. return mytools;
  213. }
  214. _ParameterPtr CDALMyBools::GetIdPara(UINT id,_CommandPtr sqlObj)
  215. {
  216. _ParameterPtr para1=sqlObj->CreateParameter("id",adInteger,adParamInput,4);
  217. para1->Value=_variant_t((long)id);
  218. return para1;
  219. }