如何用DAO创建表? 还少了m_tabledef.Append(),在之前最好先创建字段m_tabledef.CreatField()....,后才m_tabledef.Append(). 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 vcmfc:can U give me a simple but complete sample code?Thank U very much! 创建库:BOOL createDatabase(CDaoDatabase **ppDatabase, CString fileName, int dwOptions /* = 0 */){ // initialize success indicator BOOL bSuccess = TRUE; // close and delete if necessary if (*ppDatabase != NULL) { if ((*ppDatabase)->IsOpen()) closeDatabase(ppDatabase); delete *ppDatabase; } // construct new database *ppDatabase = new CDaoDatabase; // failed to allocate if (ppDatabase == NULL) return FALSE; // error // now create the database object with error checking try { // default language specified (*ppDatabase)->Create(fileName, dbLangGeneral, dwOptions); } catch (CDaoException *e) { // create a message to display CString message = _T("创建数据库失败,出现异常:"); message += e->m_pErrorInfo->m_strDescription; // output status AfxMessageBox(message); // not rethrowing, so delete exception e->Delete(); delete *ppDatabase; *ppDatabase = NULL; // failure bSuccess = FALSE; } catch (CMemoryException *e) { // output status AfxMessageBox(_T("Failed to create database--Memory exception thrown.")); // not rethrowing, so delete exception e->Delete(); delete *ppDatabase; *ppDatabase = NULL; // failure bSuccess = FALSE; } return bSuccess;}创建表:BOOL createNewTableDef(CDaoDatabase * pDatabase, CDaoTableDef **ppTableDef, CString strTableName){ // if the database is non-existent, then the answer is obvious if (pDatabase == NULL) return FALSE; // check for existing table with this name just to be safe if (IsExistentTable(pDatabase, strTableName)) { AfxMessageBox(_T("A table by that name already exists.")); // return FALSE since can't create duplicate table return FALSE; } // initialize creation failure indicator BOOL bCreateFailed = FALSE; // construct tabledef *ppTableDef = new CDaoTableDef(pDatabase); // failed to allocate so exit if ((*ppTableDef) == NULL) return FALSE; // no duplication, so create the tabledef if possible TRY { (*ppTableDef)->Create(strTableName); } CATCH (CDaoException, e) { // construct a meaningful message CString strMessage = _T("Couldn't create tabledef--Exception: "); strMessage += e->m_pErrorInfo->m_strDescription; AfxMessageBox(strMessage); // indicate failure bCreateFailed = TRUE; // delete the tabledef on failure if (*ppTableDef != NULL) { delete *ppTableDef; *ppTableDef = NULL; } } AND_CATCH (CMemoryException, e) { // output status AfxMessageBox(_T("Failed to create tabledef--Memory exception thrown.")); // indicate failure bCreateFailed = TRUE; // delete the tabledef on failure if (*ppTableDef != NULL) { delete *ppTableDef; *ppTableDef = NULL; } } END_CATCH // return TRUE if creation succeeds return (!bCreateFailed);}创建域:BOOL createNewField(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo){ // if the tabledef is non-existent, then the answer is obvious if (pTableDef == NULL) return FALSE; // check for existing field with this name just to be safe if (IsExistentField(pTableDef, pFieldInfo->m_strName)) { AfxMessageBox(_T("A field by that name already exists.")); // return FALSE since can't create duplicate field return FALSE; } // initialize failure indicators BOOL bCreateFailed = FALSE; // create a field with the specified properties // it is automatically appended to field collection // of the tabledef TRY { pTableDef->CreateField(*pFieldInfo); } CATCH (CDaoException, e) { // construct a meaningful message CString message = _T("Couldn't create field--Exception: "); message += e->m_pErrorInfo->m_strDescription; // display message AfxMessageBox(message); // indicate failure bCreateFailed = TRUE; } AND_CATCH (CMemoryException, e) { AfxMessageBox(_T("Failed to create field--Memory exception thrown.")); // indicate failure bCreateFailed = TRUE; } END_CATCH // return TRUE if creation succeeds return (!bCreateFailed);}打开表:BOOL appendTableDef(CDaoDatabase *pDatabase, CDaoTableDef *pTableDef){ // if the database is non-existent, then the answer is obvious if (pDatabase == NULL) return FALSE; // initialize success indicator BOOL bSuccess = TRUE; // append the tabledef to the collection TRY { pTableDef->Append(); } CATCH (CDaoException, e) { // construct informative message CString strMessage = _T("Couldn't append TableDef--Exception: "); strMessage += e->m_pErrorInfo->m_strDescription; // output status AfxMessageBox(strMessage); // failure bSuccess = FALSE; } AND_CATCH (CMemoryException, e) { // output status AfxMessageBox(_T("Failed to append tabledef--Memory exception thrown.")); // failure bSuccess = FALSE; } END_CATCH // return status return bSuccess;}...................................................ok! 打开表:void OpenClassTable(CDaoDatabase * m_st_data,CDaoRecordset &rs,CString tablename){ if(!m_st_data->IsOpen()) openDatabase(&m_st_data,"库名"); CString strSelect = "Select * From ["+tablename+"]"; rs.Open(dbOpenDynaset,strSelect); if(rs.GetRecordCount()!=0) rs.MoveFirst();} picture控件上绘图怪问题! VC在新建项目的时候可以选择空的makefile,但是编译之后还是会产生dsw/dsp。到底以哪个为主呢? 怎样为对话框和控件添加图片?? VC如何连接oracal数据库 有关调色板 关于控件移动的问题 关于new delete重载问题 什么软件可以撤底删除文件? 记事本(notepade.exe)打开jpg图像的问题 在WIN2000(SERVER)下如何修改本机日期(用VC)? SendMessage和PostMessage的区别? 为何不能delete此内存!
can U give me a simple but complete sample code?
Thank U very much!
BOOL createDatabase(CDaoDatabase **ppDatabase, CString fileName,
int dwOptions /* = 0 */)
{
// initialize success indicator
BOOL bSuccess = TRUE; // close and delete if necessary
if (*ppDatabase != NULL)
{
if ((*ppDatabase)->IsOpen())
closeDatabase(ppDatabase);
delete *ppDatabase;
} // construct new database
*ppDatabase = new CDaoDatabase; // failed to allocate
if (ppDatabase == NULL)
return FALSE; // error // now create the database object with error checking
try
{
// default language specified
(*ppDatabase)->Create(fileName, dbLangGeneral, dwOptions);
}
catch (CDaoException *e)
{
// create a message to display
CString message = _T("创建数据库失败,出现异常:");
message += e->m_pErrorInfo->m_strDescription; // output status
AfxMessageBox(message); // not rethrowing, so delete exception
e->Delete(); delete *ppDatabase;
*ppDatabase = NULL; // failure
bSuccess = FALSE;
}
catch (CMemoryException *e)
{
// output status
AfxMessageBox(_T("Failed to create database--Memory exception thrown.")); // not rethrowing, so delete exception
e->Delete(); delete *ppDatabase;
*ppDatabase = NULL; // failure
bSuccess = FALSE;
} return bSuccess;
}创建表:
BOOL createNewTableDef(CDaoDatabase * pDatabase,
CDaoTableDef **ppTableDef,
CString strTableName)
{
// if the database is non-existent, then the answer is obvious
if (pDatabase == NULL)
return FALSE; // check for existing table with this name just to be safe
if (IsExistentTable(pDatabase, strTableName))
{
AfxMessageBox(_T("A table by that name already exists.")); // return FALSE since can't create duplicate table
return FALSE;
} // initialize creation failure indicator
BOOL bCreateFailed = FALSE; // construct tabledef
*ppTableDef = new CDaoTableDef(pDatabase); // failed to allocate so exit
if ((*ppTableDef) == NULL)
return FALSE; // no duplication, so create the tabledef if possible
TRY
{
(*ppTableDef)->Create(strTableName);
}
CATCH (CDaoException, e)
{
// construct a meaningful message
CString strMessage = _T("Couldn't create tabledef--Exception: ");
strMessage += e->m_pErrorInfo->m_strDescription; AfxMessageBox(strMessage); // indicate failure
bCreateFailed = TRUE; // delete the tabledef on failure
if (*ppTableDef != NULL)
{
delete *ppTableDef;
*ppTableDef = NULL;
}
}
AND_CATCH (CMemoryException, e)
{
// output status
AfxMessageBox(_T("Failed to create tabledef--Memory exception thrown.")); // indicate failure
bCreateFailed = TRUE; // delete the tabledef on failure
if (*ppTableDef != NULL)
{
delete *ppTableDef;
*ppTableDef = NULL;
}
}
END_CATCH // return TRUE if creation succeeds
return (!bCreateFailed);
}创建域:BOOL createNewField(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo)
{
// if the tabledef is non-existent, then the answer is obvious
if (pTableDef == NULL)
return FALSE; // check for existing field with this name just to be safe
if (IsExistentField(pTableDef, pFieldInfo->m_strName))
{
AfxMessageBox(_T("A field by that name already exists.")); // return FALSE since can't create duplicate field
return FALSE;
} // initialize failure indicators
BOOL bCreateFailed = FALSE; // create a field with the specified properties
// it is automatically appended to field collection
// of the tabledef
TRY
{
pTableDef->CreateField(*pFieldInfo);
}
CATCH (CDaoException, e)
{
// construct a meaningful message
CString message = _T("Couldn't create field--Exception: ");
message += e->m_pErrorInfo->m_strDescription; // display message
AfxMessageBox(message); // indicate failure
bCreateFailed = TRUE;
}
AND_CATCH (CMemoryException, e)
{
AfxMessageBox(_T("Failed to create field--Memory exception thrown.")); // indicate failure
bCreateFailed = TRUE;
}
END_CATCH // return TRUE if creation succeeds
return (!bCreateFailed);
}打开表:
BOOL appendTableDef(CDaoDatabase *pDatabase, CDaoTableDef *pTableDef)
{
// if the database is non-existent, then the answer is obvious
if (pDatabase == NULL)
return FALSE; // initialize success indicator
BOOL bSuccess = TRUE; // append the tabledef to the collection
TRY
{
pTableDef->Append();
}
CATCH (CDaoException, e)
{
// construct informative message
CString strMessage = _T("Couldn't append TableDef--Exception: ");
strMessage += e->m_pErrorInfo->m_strDescription; // output status
AfxMessageBox(strMessage); // failure
bSuccess = FALSE;
}
AND_CATCH (CMemoryException, e)
{
// output status
AfxMessageBox(_T("Failed to append tabledef--Memory exception thrown.")); // failure
bSuccess = FALSE;
}
END_CATCH // return status
return bSuccess;
}
...................................................ok!
void OpenClassTable(CDaoDatabase * m_st_data,CDaoRecordset &rs,CString tablename)
{
if(!m_st_data->IsOpen())
openDatabase(&m_st_data,"库名");
CString strSelect = "Select * From ["+tablename+"]";
rs.Open(dbOpenDynaset,strSelect);
if(rs.GetRecordCount()!=0)
rs.MoveFirst();
}