DBREAD.H
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:3k
源码类别:

Windows编程

开发平台:

Visual C++

  1. #include <atldbcli.h>
  2. #if 1
  3. class CProduct
  4. {
  5. public:
  6. long    nProductID;
  7. char*   pName;
  8. long    nUnitsInStock;
  9. long    nReorderLevel;
  10. BEGIN_ACCESSOR_MAP(CProduct, 1)
  11. BEGIN_ACCESSOR(0, false)
  12. COLUMN_ENTRY(1, nProductID)
  13. COLUMN_ENTRY_TYPE(2, DBTYPE_STR | DBTYPE_BYREF, pName)
  14. COLUMN_ENTRY(7, nUnitsInStock)
  15. COLUMN_ENTRY(9, nReorderLevel)
  16. END_ACCESSOR();
  17. END_ACCESSOR_MAP()
  18. DEFINE_COMMAND(CProduct, _T("SELECT * FROM Products"))
  19. };
  20. #else
  21. class CProduct
  22. {
  23. public:
  24. long id;
  25. char Name[80];
  26. char PhoneNumber[50];
  27. char Location[50];
  28. char Title[50];
  29. char Company[50];
  30. char Email[50];
  31. BEGIN_COLUMN_MAP(CProduct)
  32. COLUMN_ENTRY(1, id)
  33. COLUMN_ENTRY(2, Name)
  34. COLUMN_ENTRY(3, PhoneNumber)
  35. COLUMN_ENTRY(4, Location)
  36. COLUMN_ENTRY(5, Title)
  37. COLUMN_ENTRY(6, Company)
  38. COLUMN_ENTRY(7, Email)
  39. END_COLUMN_MAP()
  40. };
  41. #endif
  42. void ProcessRecord(CProduct& product);
  43. class CMyRowset : public CRowset
  44. {
  45. public:
  46. HRESULT MoveAndProcess()
  47. {
  48. CProduct product;
  49. HRESULT hr;
  50. hr = MoveNext();
  51. if (hr == S_OK)
  52. {
  53. hr = GetDataHere(&product);
  54. ATLASSERT(SUCCEEDED(hr));
  55. ProcessRecord(product);
  56. }
  57. return hr;
  58. }
  59. };
  60. ULONG WINAPI ReadTable(CTable<CAccessor<CProduct>, CMyRowset>* pProduct)
  61. {
  62. CMyRowset rowset = *((CMyRowset*)pProduct);
  63. HRESULT   hr;
  64. ULONG     nRows = 0;
  65. while ((hr=rowset.MoveAndProcess()) == S_OK)
  66. {
  67. nRows++;
  68. }
  69. ATLTRACE("Thread %d processed %d records. Last HRESULT = %Xn", GetCurrentThreadId(), nRows, hr);
  70. return nRows;
  71. }
  72. void ProcessRecord(CProduct& product)
  73. {
  74. DWORD dwThreadId = GetCurrentThreadId();
  75. ATLTRACE("%d, %4d, %sn", dwThreadId, product.nProductID, product.pName);
  76. }
  77. HRESULT ReadRecords(int nThreads, DWORD* pdwTime, DWORD* pRowsProcessed)
  78. {
  79. CDataSource     db;
  80. CSession        session;
  81. CTable<CAccessor<CProduct>, CMyRowset>  product;
  82. HRESULT         hr;
  83. DWORD           dwTime;
  84. ATLTRACE("DoStuff: Startedn");
  85. hr = db.Open(_T("MSDASQL"), _T("OLE_DB_NWind_Jet"));
  86. ATLASSERT(SUCCEEDED(hr));
  87. if (FAILED(hr))
  88. return hr;
  89. hr = session.Open(db);
  90. ATLASSERT(SUCCEEDED(hr));
  91. if (FAILED(hr))
  92. return hr;
  93. CDBPropSet propset(DBPROPSET_ROWSET);
  94. propset.AddProperty(DBPROP_CANHOLDROWS, true);
  95. hr = product.Open(session, _T("Products"), &propset);
  96. ATLASSERT(SUCCEEDED(hr));
  97. if (FAILED(hr))
  98. return hr;
  99. DWORD  idThread;
  100. int i;
  101. HANDLE* phThread = new HANDLE[nThreads];
  102. if (phThread == NULL)
  103. return E_FAIL;
  104. dwTime = GetTickCount();
  105. for (i=0; i<nThreads; i++)
  106. {
  107. *(phThread+i) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadTable, &product, 0, &idThread);
  108. if (*(phThread+i) == NULL)
  109. ATLTRACE(_T("Couldn't create thread %dn"), i);
  110. }
  111. // Ensure all threads are finished before we end
  112. WaitForMultipleObjects(nThreads, phThread, TRUE, INFINITE);
  113. // Return the total time
  114. *pdwTime = GetTickCount() - dwTime;
  115. DWORD nTotalRows = 0, nRows;
  116. for (i=0; i<nThreads; i++)
  117. {
  118. GetExitCodeThread(*(phThread + i), &nRows);
  119. nTotalRows += nRows;
  120. CloseHandle(*(phThread + i));
  121. }
  122. *pRowsProcessed = nTotalRows;
  123. ATLTRACE("DoStuff: Endedn");
  124. return S_OK;
  125. }