如何从 CDBVariant 获取日期?
how to get date from CDBVariant?
我正在尝试使用 CDBVariant 从 sql 服务器获取日期、月份、年份,但我无法做到这一点,我的代码如下
我想要的只是用我自己的格式计算日月年
像这样
3 jan 1990
在我的学生 table 中,我使用日期类型而不是 date/time 所以我只想以我自己的格式计算日期,因为我从数据库中获取的日期是 年-month-date 我想显示为 date-month-year
我无法将 CDBVariant::m_pdate 转换为 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
}
我正在将 CTime 设置为 NULL,以便可以插入下一个值,因为 ctime 第一次显示正常但之后它开始添加它不知道为什么我想要的只是从数据库中获取日期并计算它。
将 CDBVariant
转换为 COleDateTime
的正确方法如下:
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"));
}
就我个人而言,我发现 CDBVariant::m_dwType
报告的数据类型在与 SQL 数据一起使用时有点不可靠。
您最好的办法可能是首先使用 CRecordset::GetODBCFieldInfo
函数检查 CDBVariant 中返回的是哪种 (SQL) 数据,然后 "tell" CRecordset::GetFieldValue()
默认C数据类型:
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
}
}
但是,如果您不使用 CRecordSet,并且仅使用 CDBVariant 来在运行时处理任何类型的数据,那么上述内容可能不会有任何帮助。
我正在尝试使用 CDBVariant 从 sql 服务器获取日期、月份、年份,但我无法做到这一点,我的代码如下
我想要的只是用我自己的格式计算日月年 像这样
3 jan 1990
在我的学生 table 中,我使用日期类型而不是 date/time 所以我只想以我自己的格式计算日期,因为我从数据库中获取的日期是 年-month-date 我想显示为 date-month-year
我无法将 CDBVariant::m_pdate 转换为 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
}
我正在将 CTime 设置为 NULL,以便可以插入下一个值,因为 ctime 第一次显示正常但之后它开始添加它不知道为什么我想要的只是从数据库中获取日期并计算它。
将 CDBVariant
转换为 COleDateTime
的正确方法如下:
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"));
}
就我个人而言,我发现 CDBVariant::m_dwType
报告的数据类型在与 SQL 数据一起使用时有点不可靠。
您最好的办法可能是首先使用 CRecordset::GetODBCFieldInfo
函数检查 CDBVariant 中返回的是哪种 (SQL) 数据,然后 "tell" CRecordset::GetFieldValue()
默认C数据类型:
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
}
}
但是,如果您不使用 CRecordSet,并且仅使用 CDBVariant 来在运行时处理任何类型的数据,那么上述内容可能不会有任何帮助。