Access中的数据类型在ODBC中问题!
点击次数:31 次 发布日期:2008-11-27 18:43:56 作者:源代码网
|
源代码网推荐
源代码网整理以下
由于想在Access和MySQL数据互导,在自动建表中需要知道各属性的数据类型。可是在Access中数据类型为 文本 可是通过测试出来的数据类型不一,有时为DBVT_NULL,有时为DBVT_STRING,有时为DBVT_LONG。那我该怎么办啊?
具体测试可通过一个按钮响应函数和ODBC设置实现:
void CButtonDlg::OnButton1()
{
// TODO: Add your control notification handler code here
// Create and open a database object;
// do not load the cursor library
CDatabase db;
db.OpenEx( _T( "DSN=Record" ), //数据源为Record
CDatabase::openReadOnly |
CDatabase::noOdbcDialog );
// Create and open a recordset object
// directly from CRecordset. Note that a
// table must exist in a connected database.
// Use forwardOnly type recordset for best
// performance, since only MoveNext is required
CRecordset rs( &db );
rs.Open( CRecordset::forwardOnly,
_T( "SELECT * FROM Record" ) );//其中表名为Record
// Create a CDBVariant object to
// store field data
CDBVariant varValue;
// Loop through the recordset,
// using GetFieldValue and
// GetODBCFieldCount to retrieve
// data in all columns
short nFields = rs.GetODBCFieldCount( );
while( !rs.IsEOF( ) )
{
for( short index = 0; index < nFields; index++ )
{
rs.GetFieldValue( index, varValue );
CString str;
str.Format("The type is %d. %d", varValue.m_dwType, nFields);
AfxMessageBox(str);
// do something with varValue
}
}
rs.MoveNext( );
}
rs.Close( );
db.Close( );
}
---------------------------------------------------------------
很正常,因为这个字段里面有null植,有文本,比如aa965135
还有可以转换为数字的文本,比如:0005655156
源代码网供稿. |