Department.H
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:3k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // Department.H : Declaration of the CDepartment class
  2. #ifndef __DEPARTMENT_H_
  3. #define __DEPARTMENT_H_
  4. class CDepartmentAccessor
  5. {
  6. public:
  7. TCHAR m_DepartmentCode[5]; TCHAR m_DepartmentName[51];
  8. BEGIN_COLUMN_MAP(CDepartmentAccessor)
  9. COLUMN_ENTRY(1, m_DepartmentCode) COLUMN_ENTRY(2, m_DepartmentName) END_COLUMN_MAP()
  10. DEFINE_COMMAND(CDepartmentAccessor, _T("  SELECT  DepartmentCode,  DepartmentName   FROM Department"))
  11. // You may wish to call this function if you are inserting a record and wish to
  12. // initialize all the fields, if you are not going to explicitly set all of them.
  13. void ClearRecord()
  14. {
  15. memset(this, 0, sizeof(*this));
  16. }
  17. };
  18. class CDepartment : public CCommand<CAccessor<CDepartmentAccessor> >
  19. {
  20. public:
  21. //Constructor added by Chuck Wood
  22. CDepartment () {
  23. //Allow 1 k for SQL WHERE clause
  24. m_strFilter = new char[1];
  25. strcpy (m_strFilter, "");
  26. }
  27. //Destructor added by Chuck Wood
  28. ~CDepartment () {
  29. //Release resources
  30. delete m_strFilter;
  31. }
  32. HRESULT Open()
  33. {
  34. HRESULT hr;
  35. hr = OpenDataSource();
  36. if (FAILED(hr))
  37. return hr;
  38. return OpenRowset();
  39. }
  40. HRESULT OpenDataSource()
  41. {
  42. HRESULT hr;
  43. CDataSource db;
  44. CDBPropSet dbinit(DBPROPSET_DBINIT);
  45. dbinit.AddProperty(DBPROP_AUTH_CACHE_AUTHINFO, true); dbinit.AddProperty(DBPROP_AUTH_ENCRYPT_PASSWORD, false); dbinit.AddProperty(DBPROP_AUTH_MASK_PASSWORD, false); dbinit.AddProperty(DBPROP_AUTH_PASSWORD, OLESTR("")); dbinit.AddProperty(DBPROP_AUTH_PERSIST_ENCRYPTED, false); dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false); dbinit.AddProperty(DBPROP_AUTH_USERID, OLESTR("Admin")); dbinit.AddProperty(DBPROP_INIT_DATASOURCE, OLESTR("C:\My Documents\Visual C++\Classes.mdb")); dbinit.AddProperty(DBPROP_INIT_MODE, (long)16); dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4); dbinit.AddProperty(DBPROP_INIT_PROVIDERSTRING, OLESTR(";COUNTRY=0;CP=1252;LANGID=0x0409")); dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033); hr = db.Open(_T("Microsoft.Jet.OLEDB.3.51"), &dbinit);
  46. if (FAILED(hr))
  47. return hr;
  48. return m_session.Open(db);
  49. }
  50. HRESULT OpenRowset()
  51. {
  52. // Set properties for open
  53. CDBPropSet propset(DBPROPSET_ROWSET);
  54. propset.AddProperty(DBPROP_IRowsetChange, true);
  55. propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);
  56. //The rest of this function was added by Chuck Wood
  57. //Allow 512 bytes for the new SQL Command
  58. char newSQL[512];
  59. char *SQLCommand;
  60. GetDefaultCommand((const char **) &SQLCommand);
  61. strcpy(newSQL, SQLCommand);
  62. if (strlen(m_strFilter)) {
  63. strcat(newSQL, " WHERE ");
  64. strcat(newSQL, m_strFilter);
  65. }
  66. return CCommand<CAccessor<CDepartmentAccessor> >
  67. ::Open(m_session, newSQL, &propset);
  68. // return CCommand<CAccessor<CDepartmentAccessor> >::Open(m_session, NULL, &propset);
  69. }
  70. char   *m_strFilter;
  71. CSession m_session;
  72. };
  73. #endif // __DEPARTMENT_H_