|
VC++6.0 + ADO+ Access2000 + SQL_Update ??
#32335054
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: Space
Сообщения: 485
|
|
В MSDN нашел вот что:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232.
// BeginOpenCpp
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename( "EOF" , "EndOfFile" )
#include <oledb.h>
#include <stdio.h>
#include <conio.h>
#include "OpenX.h"
// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void OpenX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
///////////////////////////////////////////////////////////
// //
// Main Function //
// //
///////////////////////////////////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
OpenX();
::CoUninitialize();
}
///////////////////////////////////////////////////////////
// //
// OpenX Function //
// //
///////////////////////////////////////////////////////////
void OpenX(void)
{
// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace
_RecordsetPtr pRstEmployee = NULL;
_ConnectionPtr pConnection = NULL;
// Define string variables.
_bstr_t strCnn( "Provider='sqloledb';Data Source='MySqlServer';"
"Initial Catalog='pubs';Integrated Security='SSPI';" );
// Define Other Variables.
HRESULT hr = S_OK;
IADORecordBinding *picRs = NULL; // Interface Pointer declared.
CEmployeeRs emprs; // C++ Class object
DBDATE varDate;
try
{
// open connection and record set
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->Open(strCnn, ""," ",adConnectUnspecified);
TESTHR(pRstEmployee.CreateInstance(__uuidof(Recordset)));
pRstEmployee->Open(" Employee ",
_variant_t((IDispatch *)pConnection,true), adOpenKeyset,
adLockOptimistic, adCmdTable);
// Open an IADORecordBinding interface pointer which we'll
// use for Binding Recordset to a class.
TESTHR(pRstEmployee->QueryInterface(
__uuidof(IADORecordBinding),(LPVOID*)&picRs));
//Bind the Recordset to a C++ Class here.
TESTHR(picRs->BindToRecordset(&emprs));
// Assign the first employee record's hire date
// to a variable, then change the hire date.
varDate = emprs.m_sze_hiredate;
printf(" \nOriginal data\n ");
printf(" \tName - Hire Date\n ");
printf(" %s %s - %d/%d/%d\n\n ",
emprs.le_fnameStatus == adFldOK ?
emprs.m_sze_fname : " <NULL> ",
emprs.le_lnameStatus == adFldOK ?
emprs.m_sze_lname : " <NULL> ",
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.month : 0 ,
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.day : 0 ,
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.year : 0 );
emprs.m_sze_hiredate.year= 1900 ;
emprs.m_sze_hiredate.month= 1 ;
emprs.m_sze_hiredate.day= 1 ;
picRs->Update(&emprs);
printf("\nChanged data\n ");
printf(" \tName - Hire Date\n ");
printf(" %s %s - %d/%d/%d\n\n ",
emprs.le_fnameStatus == adFldOK ?
emprs.m_sze_fname : " <NULL> ",
emprs.le_lnameStatus == adFldOK ?
emprs.m_sze_lname : " <NULL> ",
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.month : 0 ,
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.day : 0 ,
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.year : 0 );
// Requery Recordset and reset the hire date.
pRstEmployee->Requery(adOptionUnspecified);
// Open an IADORecordBinding interface pointer which we'll
// use for Binding Recordset to a class.
TESTHR(pRstEmployee->QueryInterface(
__uuidof(IADORecordBinding),(LPVOID*)&picRs));
// Rebind the Recordset to a C++ Class here.
TESTHR(picRs->BindToRecordset(&emprs));
emprs.m_sze_hiredate = varDate;
picRs->Update(&emprs);
printf("\nData after reset\n");
printf("\tName - Hire Date\n");
printf(" %s %s - %d/%d/%d",emprs.le_fnameStatus == adFldOK ?
emprs.m_sze_fname : "<NULL>",
emprs.le_lnameStatus == adFldOK ?
emprs.m_sze_lname : "<NULL>",
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.month : 0,
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.day : 0,
emprs.le_hiredateStatus == adFldOK ?
emprs.m_sze_hiredate.year : 0);
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Connection.
PrintProviderError(pConnection);
PrintComError(e);
}
// Clean up objects before exit.
if (pRstEmployee)
if (pRstEmployee->State == adStateOpen)
pRstEmployee->Close();
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
}
///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
if( (pConnection->Errors->Count) > 0 )
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount - 1 .
for(long i = 0 ;i < nCount;i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("\t Error number: %x\t%s ", pErr->Number,
pErr->Description);
}
}
}
///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print COM errors.
printf(" Error\n ");
printf(" \tCode = %08lx\n ", e.Error());
printf(" \tCode meaning = %s\n ", e.ErrorMessage());
printf(" \tSource = %s\n ", (LPCSTR) bstrSource);
printf(" \tDescription = %s\n ", (LPCSTR) bstrDescription);
}
// EndOpenCpp
OpenX.h:
// BeginOpenH
#include " icrsint.h"
// This Class extracts only fname,lastname and
// hire_date from employee table
class CEmployeeRs : public CADORecordBinding
{
BEGIN_ADO_BINDING(CEmployeeRs)
// Column fname is the 2nd field in the table
ADO_VARIABLE_LENGTH_ENTRY2( 2 , adVarChar, m_sze_fname,
sizeof(m_sze_fname), le_fnameStatus, FALSE)
// Column lname is the 4th field in the table.
ADO_VARIABLE_LENGTH_ENTRY2( 4 , adVarChar, m_sze_lname,
sizeof(m_sze_lname), le_lnameStatus, FALSE)
// Column hiredate is the 8th field in the table.
ADO_VARIABLE_LENGTH_ENTRY2( 8 , adDBDate,m_sze_hiredate,
sizeof(m_sze_hiredate), le_hiredateStatus, TRUE)
END_ADO_BINDING()
public:
CHAR m_sze_fname[ 21 ];
ULONG le_fnameStatus;
CHAR m_sze_lname[ 31 ];
ULONG le_lnameStatus;
DBDATE m_sze_hiredate;
ULONG le_hiredateStatus;
};
// EndOpenH
При компиляции в VC++6.0 пишет ошибку:
d:\myprojects.vc6\ado\debug\msado15.tlh(407) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
D:\MyProjects.VC6\Ado\Ado.cpp(13) : fatal error C1083: Cannot open include file: 'OpenX.h': No such file or directory
Error executing cl.exe.
Ado.exe - 1 error(s), 1 warning(s)
При компиляции в VC++7.0 пишет ошибку: d:\MyProjects.NET\Programs\test\c#\ADO2\ADO2.cpp(9): fatal error C1083: Cannot open include file: 'OpenX.h': No such file or directory
|
|
|