TRY
{
// Open the database using the former created pseudo DSN
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL string
// Remember to name a section of data in the Excel sheet using "Insert->Names" to be
// able to work with the data like you would with a table in a "real" database. There
// may be more than one table contained in a worksheet.
sSql = "SELECT * from [Sheet1$]";
// Execute that query (implicitly by opening the recordset)
recset.Open(CRecordset::forwardOnly,sSql,CRecordset::readOnly);
// Browse the result
iCount = 0;
short nFields = recset.GetODBCFieldCount( );
while( !recset.IsEOF() )
{
// Read the result line
for( short index = 0; index < nFields; index++ )
{
// do something with varValu
switch (index)
{
case 0:
recset.GetFieldValue(index, sItem1);
sprintf(name, "%s", sItem1);
break;
case 1:
recset.GetFieldValue(index, sItem2);
sprintf(num, "%s", sItem2.Left(sItem2.GetLength()-2));
break;
default:
break;
}
}
if (strlen(num)!=11)
{
MessageBox("导入的手机号位数不正确,将不允许被导入","联系簿导入");
}
else
{
InsertList(num, name, "", "");
iCount++;
}
// Skip to the next resultline
recset.MoveNext();
}
// Close the database
sprintf(msg, "导入成功,本次共计导入%d条记录", iCount);
MessageBox(msg,"批量发送");
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}