Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How does one save and retrieve date values from sql server ce in c++?

Ok.I have to get date from month_cal control in form of a SYSTEMTIME structure and save into sqlserver ce database.How do I do this.
I thought of adding a parameter like shown below but don't know how to construct the variant value.Since it a date parameter I am creating, does adding a string in the formate dd/mm/yyyy make sense?
Also, I need to get saved date from data base and use it to update a monthcal control using systen time.How do I retrive the date value considering that c++ has no date data type(I know sql server does)

Relevant input code fragment is:
Cmd->Parameters->Append(Cmd->CreateParameter(_bstr_t(_T("Name")),adDate,adParamInput, -1, _variant_t(SOME_VALUE_WHOSE_C++_TYPE_IS_UNKNOWN_TO_ME)));


Relevant output code fragment is:
SOME_UNKNOWN_C++_TYPE_TO_ME Result = (SOME_UNKNOWN_C++_TYPE_TO_ME)Rs->Feilds->Item[0]->Value;
Posted
Updated 22-Mar-14 1:33am
v4

You should always use DateTime types in your database. Never use strings for storing dates, as it creates more problems than it solves. You can use the COleDateTime Class[^] in C++.
 
Share this answer
 
Comments
Gbenbam 22-Mar-14 8:06am    
Thanks a lot.But please can the COleDateTime be used in a WIN32 c++ project type too.Or is it restricted to only MFC prohects.
Richard MacCutchan 22-Mar-14 8:59am    
It is a shared class and can be used via the Active Template Library.
The _variant_t class has a date member. Because dates are stored as double values, you must use a datetime type that uses the same encoding like COleDateTime and DATE. Then you can assigne the date value:
SYSTEMTIME st;
COleDateTime odt(st);
_variant_t vt;
vt.vt = VT_DATE;
vt.date = odt;

DATE dt;
::SystemTimeToVariantTime(&st, &dt);
vt.vt = VT_DATE;
vt.date = dt;
 
Share this answer
 
Comments
Gbenbam 22-Mar-14 8:12am    
Thanks a lot.These your code snippets address only am attempt to save date into a database.Could you kindly help with matching code snippets for retrieving date type from a variant that was holds a date type I.e one with vt = VT_DATE.

Does a VariantTypeToSystemTime function exist?
Jochen Arndt 22-Mar-14 8:17am    
Just check for the DATE type and use the value:
if (VT_DATE == vt.vt)
VariantTimeToSystemTime(vt.date, &st);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900