1

i am trying to get the the day ,month ,year from sql server using CDBVariant but i cant able to do that my code is below

what i want is just cout the day ,month ,year in my own format like this

3 jan 1990

in my student table i use date type not the date/time so i want to just cout the date in my own format because the date i get from database is year-month-date i want to show it as date-month-year

I cant able to convert CDBVariant::m_pdate to ctime

    CDBVariant date; 
    rs.GetFieldValue(short(2),date);//

    CTime a(date.m_pdate->year, date.m_pdate->month, date.m_pdate->day);

    cout << " date is:" << a.GetYear() << endl;
    a=NULL; //so the next value can be insert into ctime                
    rs.MoveNext(); // move to next tuple in table
}

i am making CTime a NULL so the next value can be insert becuase ctime show first time ok but after that it start adding on it dont know why all i want is just get the date from database and cout it.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • What **is** the problem? Also consider editing your question, removing code that is unrelated to your problem. – IInspectable Dec 14 '15 at 23:21
  • Avoid using `system("pause")`. Use `cin.get();` instead. – Andrew Komiagin Dec 15 '15 at 04:58
  • 1
    `a=NULL;` doesn't do what you think it does. You need to learn about object lifetimes and that objects aren't pointers. Consider picking up one or more books from [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Dec 15 '15 at 09:56
  • the data i am getting from database is 2015-12-15 in CDBVariant date so how i can convert that into CTime variable –  Dec 15 '15 at 22:27

2 Answers2

1

The proper way to convert CDBVariant to COleDateTime is the following:

CDBVariant var;

if(var.m_dwType == DBVT_DATE) 
{
    COleDateTime timestamp(var.m_pdate->year,var.m_pdate->month,var.m_pdate->day,
                     var.m_pdate->hour,var.m_pdate->minute,var.m_pdate->second);
    CString str = timestamp.Format(_T("%B %d, %Y"));
}
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • It does actually work. You should use `CString str = timestamp.Format(_T("%B %d, %Y"));` to get the printable string. – Andrew Komiagin Dec 16 '15 at 07:18
  • the problem was that the data i was getting from database was not pointing to the date part so i use ``rs.GetFieldValue(L"date",date, SQL_C_TIMESTAMP);`` –  Dec 16 '15 at 16:08
0

Personally, I've found the data type reported by CDBVariant::m_dwType to be a bit dodgy when used in conjunction with SQL data.

You're best bet is probably to check what kind of (SQL) data is being returned in the CDBVariant first, using the CRecordset::GetODBCFieldInfo function, and then "tell" CRecordset::GetFieldValue() the default C data type:

CRecordSet oSet;        // Hold the set of records returned by SQL query
CDBVariant oVar;        // get the variant values from the each of the fields in SQL resultSet
CODBCFieldInfo oFinfo;  // to get the fieldinfo reported by ODBC
TIMESTAMP_STRUCT myDtm; // DateTime structure for accessing the components of dateTimes from SQL

...     // some other code to populate the oSet

cnsNumOsetCols = oSet.GetODBCFieldCount();

// Iterate thru the columns in the oSet
for (idxCol = 0; idxCol < cnsNumOsetCols; idxCol++)
{
    // Use the ODBC info from the resultSet
    oSet.GetODBCFieldInfo((short)idxCol, oFinfo);
    if ((oFinfo.m_nSQLType == SQL_DATE))
    {
        // Specify the C data type when retreiving the data from the variant
        oSet.GetFieldValue((short)idxCol, oVar, SQL_C_DATE);
        myDtm = oVar.m_pdate;

        ... // some other code to manipulate the date
    }
}


However, if you're not using a CRecordSet, and only using the CDBVariant for its ability to handle any kind of data at runtime, then the above probably isn't goint to be of any help.

sinecospi
  • 47
  • 2