how to dynamically set the CRecordset Fieldtype ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: how to dynamically set the CRecordset Fieldtype ?

  1. #1
    Join Date
    Aug 2014
    Posts
    13

    Question how to dynamically set the CRecordset Fieldtype ?

    Hi
    the problem i am having is that when i try to get
    Code:
     short nFields = rs.GetODBCFieldCount();
    			 CDBVariant b= new CDBVariant[nFields];
    			 for (short index = 0; index < nFields; index++)
    			 {
    				 rs.GetFieldValue(index, b[index],DEFAULT_FIELD_TYPE);		
    			 }
    now i want to change the DEFAULT_FIELD_TYPE dynamically
    so i fount out that using
    CODBCFieldInfo c;
    rs.GetODBCFieldInfo(index, c);
    SQL_C_CHAR h = c.m_nSQLType; //this is not correct but i need a SQL_C_CHAR to pass in rs.GetFieldValue(,,here);

    so how i can dynamically pass the nFieldType becuase if i dont the GetFieldValue get the value in binary which is not correct

    the data i am getting is in this form
    1 jack 23 2015-12-13
    2 ryan 23 2015-12-13
    3 jhon 23 2015-12-13
    so the GetFieldValue (,,nFieldType ) should change but its not changing
    i get the first field ok mean i get the 1 correctly but the name is in binary and other 2 values also in binary

    nFieldType SQL_C_CHAR table is link below
    https://msdn.microsoft.com/en-us/library/5f8k59f9.aspx

    if i go manually then
    for first row

    Code:
    CDBVariant id;
    CDBVariant name;
    CDBVariant age;
    CDBVariant date;
     rs.GetFieldValue(short(0), id,      SQL_C_SLONG);
     rs.GetFieldValue(short(1), name, SQL_C_CHAR);
     rs.GetFieldValue(short(2), age,    SQL_C_SLONG);
     rs.GetFieldValue(short(3), date,   SQL_C_TIMESTAMP);
    but i want to change rs.GetFieldValue(short(0), id,SQL_C_SLONG); this dynamically
    Last edited by tjnapster555; December 16th, 2015 at 02:27 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,473

    Re: how to dynamically set the CRecordset Fieldtype ?

    SQLColumns will give you information about columes including the data type.

    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

  3. #3
    Join Date
    Aug 2014
    Posts
    13

    Re: how to dynamically set the CRecordset Fieldtype ?

    can you tell me how to pass the #define type variable because in my code here
    rs.GetODBCFieldInfo(index, c);
    int h = c.m_nSQLType;
    this i get for first column in row 4 which is correct its long type so now how i can the send this SQL_C_SLONG

    how to check it ?



    Name:  2015-12-17 01_34_47-CRecordset__GetFieldValue.jpg
Views: 68
Size:  29.1 KB

  4. #4
    Join Date
    Aug 2014
    Posts
    13

    Re: how to dynamically set the CRecordset Fieldtype ?

    or if i make a function where it use if operator to the check the appropriate #define value but how i can make #define variable and then return it to pass in rs.GetFieldValue();

    for example for first colum is id (int) i using rs.GetODBCFieldInfo(index, c);
    and then int h== c.m_nSQLType; now h is 4 then i go to my fn and then check the if then
    #define a =SQL_C_SLONG;
    return should return the a variable so i can pass it in rs.GetFieldValue(,,a <-here);

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,452

    Re: how to dynamically set the CRecordset Fieldtype ?

    Quote Originally Posted by tjnapster555 View Post
    now i want to change the DEFAULT_FIELD_TYPE dynamically
    . . .
    SQL_C_CHAR h = c.m_nSQLType; //this is not correct but i need a SQL_C_CHAR to pass in rs.GetFieldValue(,,here);
    Could you please explain what exactly you're trying to achieve with that?

    It looks like the targeted db column data type is LONG. To receive corresponding record field you should specify some C data type SQL driver is able to fill in with the data from db record. In this case, SQL_C_SLONG is the best candidate.

    But you're trying to insist on SQL_C_CHAR. The type implies that SQL driver has to convert LONG to null-terminated C string. (BTW, SQL driver may or may not support this type of conversion, depending on sql driver vendor)

    Which brings us back to my first question: what exactly you're trying to achieve with that Long to Char cheat?
    Best regards,
    Igor

  6. #6
    Join Date
    Aug 2014
    Posts
    13

    Re: how to dynamically set the CRecordset Fieldtype ?

    Quote Originally Posted by Igor Vartanov View Post
    Could you please explain what exactly you're trying to achieve with that?

    It looks like the targeted db column data type is LONG. To receive corresponding record field you should specify some C data type SQL driver is able to fill in with the data from db record. In this case, SQL_C_SLONG is the best candidate.

    But you're trying to insist on SQL_C_CHAR. The type implies that SQL driver has to convert LONG to null-terminated C string. (BTW, SQL driver may or may not support this type of conversion, depending on sql driver vendor)

    Which brings us back to my first question: what exactly you're trying to achieve with that Long to Char cheat?
    well i want to send the right sql_c_char to GetFieldValue because when i try to get the date its not getting the date but it get the data in binary but if i manually set the
    GetFieldValue (,,SQL_C_TIMESTAMP) to this i get the date


    so how can send the right SQL_C_CHAR data type dynamically instead of manually every-time or how to do it using if

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,473

    Re: how to dynamically set the CRecordset Fieldtype ?

    Quote Originally Posted by tjnapster555 View Post
    well i want to send the right sql_c_char to GetFieldValue because when i try to get the date its not getting the date but it get the data in binary but if i manually set the
    GetFieldValue (,,SQL_C_TIMESTAMP) to this i get the date


    so how can send the right SQL_C_CHAR data type dynamically instead of manually every-time or how to do it using if
    Is there a reason SQLColumns won't work for you? It'll show you the type of each column. Use it to get the type and pass it to your fetch call.

  8. #8
    Join Date
    Aug 2014
    Posts
    13

    Re: how to dynamically set the CRecordset Fieldtype ?

    Quote Originally Posted by GCDEF View Post
    Is there a reason SQLColumns won't work for you? It'll show you the type of each column. Use it to get the type and pass it to your fetch call.
    can you tell me how to fetch and pass the sql_c_char
    i don't understand this from Microsoft
    https://msdn.microsoft.com/en-us/library/5f8k59f9.aspx
    Otherwise, you can specify the data type directly or choose a compatible data type; for example, you can store any data type into SQL_C_CHAR.


    how to store in sql_c_char this is what i want to do

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,452

    Re: how to dynamically set the CRecordset Fieldtype ?

    According to documentation, there is a special GetFieldValue prototype accepting CString. Using that you are going to get string value no matter what real data type it refers.
    Best regards,
    Igor

  10. #10
    Join Date
    Aug 2014
    Posts
    13

    Re: how to dynamically set the CRecordset Fieldtype ?

    Quote Originally Posted by Igor Vartanov View Post
    According to documentation, there is a special GetFieldValue prototype accepting CString. Using that you are going to get string value no matter what real data type it refers.
    but what if i get a date value from database it will be like this year/month/day means 2015/02/23 because database save it like that and what if i use the date/time-stamp data type in sql server then how i can format it like this tue 23,jan 2015 to cout it to console

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,452

    Re: how to dynamically set the CRecordset Fieldtype ?

    Database stores timestamp format in its internal binary (not a string) representation never depending on location/culture format. Therefore, you need to obtain it in the same culture-independent format, which is SQL_C_TIMESTAMP, and then format the output string yourself, using CTime::Format for example.
    Last edited by Igor Vartanov; December 22nd, 2015 at 02:11 AM.
    Best regards,
    Igor

  12. #12
    Join Date
    Aug 2014
    Posts
    13

    Question Re: how to dynamically set the CRecordset Fieldtype ?

    Quote Originally Posted by Igor Vartanov View Post
    Database stores timestamp format in its internal binary (not a string) representation never depending on location/culture format. Therefore, you need to obtain it in the same culture-independent format, which is SQL_C_TIMESTAMP, and then format the output string yourself, using CTime::Format for example.
    the database table schema is this
    id int
    name varchar(30)
    age int
    s_date date

    this is what i was asking in my question the problem is i am getting the values in array

    Code:
                             short nFields = rs.GetODBCFieldCount();
    			 CDBVariant b= new CDBVariant[nFields];
    			 for (short index = 0; index < nFields; index++)
    			 {
    				 rs.GetFieldValue(index, b[index],DEFAULT_FIELD_TYPE);		
    			 }
    now to output it
    Code:
    int id= b[0].m_iVal;
    string name=b[1].m_pstringA;
    int age =b[2].m_iVal;
    ctime a=b[3].m_pdate;

    now the date is not getting correctly it show some binary form or pointer type output like 132205FA


    how to get it correctly the date ?
    Last edited by tjnapster555; December 22nd, 2015 at 02:20 AM.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,452

    Re: how to dynamically set the CRecordset Fieldtype ?

    Code:
    #include <windows.h>
    #include <atltime.h>
    
    
    int main()
    {
        CTime a = CTime::GetCurrentTime();
        printf("%s\n", a.Format("%#c"));
        printf("%s\n", a.Format("%a %d, %b %Y %H:%M:%S"));
    }
    Code:
    J:\Temp\107>107.exe
    Tuesday, December 22, 2015 14:02:17
    Tue 22, Dec 2015 14:02:17
    Last edited by Igor Vartanov; December 22nd, 2015 at 05:13 AM.
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center