新建表:首先打开或新建一个数据库,在该数据库上使用CDaoTableDef的构赵函数定义一个对象,在调用Create函数建新表,之后调用Append函数加入数据库:
void ChangeUserPassword( ){
CString strSystemDB =
_T( "路径" )
SetSystemDB( strSystemDB )
// User name and password manually added
// by using Microsoft Access
CString strUserName = _T( "NewUser" )
CString strOldPassword = _T( "Password" )
CString strNewPassword = _T( "NewPassword" )
SetDefaultUser( strUserName, strOldPassword )
ChangePassword( strUserName, strOldPassword,
strNewPassword )
}
//设置默认用户和密码
void SetDefaultUser(CString & strUserName, CString & strPassword)
{
COleVariant varUserName( strUserName, VT_BSTRT )
COleVariant varPassword( strPassword, VT_BSTRT )
DAODBEngine* pDBEngine = AfxDaoGetEngine( )
ASSERT( pDBEngine != NULL )
// 设置默认用户
DAO_CHECK( pDBEngine->put_DefaultUser( varUserName.bstrVal ) )
// 设置默认密码
DAO_CHECK( pDBEngine->put_DefaultPassword( varPassword.bstrVal ) )
}
///更改密码
void SetDBPassword( LPCTSTR pDB, LPCTSTR pszOldPassword, LPCTSTR pszNewPassword )
{
CDaoDatabase db
CString strConnect( _T( "pwd=" ) )
// 数据库必须打开
// 设置密码
db.Open( pDB, TRUE, FALSE,
strConnect + pszOldPassword )
COleVariant NewPassword( pszNewPassword, VT_BSTRT ),
OldPassword( pszOldPassword, VT_BSTRT )
DAO_CHECK( db.m_pDAODatabase->NewPassword( V_BSTR( &OldPassword ),
V_BSTR( &NewPassword ) ) )
db.Close()
}
用Dao就可以:包含头文件#include"afxdao.h"
然后用如下代码:
CDaoDatabase m_DaoDB
try
{
m_DaoDB.Create(_T("MsgInfo.mdb"))//创建mdb文件
}
catch(CDaoException* e)
{
CString strMsg
strMsg.Format(_T(" 创建数据库失败! 原因:%s (%d)\n\n"),
(LPCTSTR)e->m_pErrorInfo->m_strDescription,
e->m_pErrorInfo->m_lErrorCode)
AfxMessageBox(strMsg,MB_OK|MB_ICONSTOP)
return false
}
CDaoTableDef m_DaoDef(&m_DaoDB)
m_DaoDef.Create(_T("工资信息"))//在刚刚创建的mdb文件中创建一个数据表
//创建字段
m_DaoDef.CreateField(_T("工号"),dbText,36,dbVariableField)
m_DaoDef.CreateField(_T("姓名"),dbText,38,dbVariableField)
m_DaoDef.CreateField(_T("工资"),dbBoolean,1,dbUpdatableField)
m_DaoDef.Append()
m_DaoDef.Close()
m_DaoDB.Close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)