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
Quote:
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
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
1 Attachment(s)
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 ?
Attachment 34035
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);
Re: how to dynamically set the CRecordset Fieldtype ?
Quote:
Originally Posted by
tjnapster555
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?
Re: how to dynamically set the CRecordset Fieldtype ?
Quote:
Originally Posted by
Igor Vartanov
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
Re: how to dynamically set the CRecordset Fieldtype ?
Quote:
Originally Posted by
tjnapster555
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.
Re: how to dynamically set the CRecordset Fieldtype ?
Quote:
Originally Posted by
GCDEF
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
Quote:
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
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.
Re: how to dynamically set the CRecordset Fieldtype ?
Quote:
Originally Posted by
Igor Vartanov
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
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.
Re: how to dynamically set the CRecordset Fieldtype ?
Quote:
Originally Posted by
Igor Vartanov
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 ?
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