WinCE自带数据库的问题

MyLar 2008-08-07 01:54:22
我最近想做一个WinCE自带的数据库的程序,用来做个人的账务管理,在网上看了好多资料了,照着做就是没有成功,请各位高手们不吝赐教。下面是我看到的一些代码。
PCEGUID CeGuid, CeGuid1;
CEOID CeOID, CeOID1;
CEDBASEINFOEX CeDBaseInfoEx;

BOOL bCre = CeMountDBVol(CeGuid, _T("\\Test.cdb"), OPEN_ALWAYS);(1)
DWORD error = ::GetLastError();(2)

HANDLE CDataBaseTestDlg::OpenDatabase(
HWND hwndNotify, // Handle to the window to which
// notification messages are posted.
PCEGUID pceguid, // Pointer to the mounted database
// volume in which the database that
// will be opened resides.
CEOID CeOid) // Object identifier of the database
// to be opened.
{
int index;
DWORD dwError; // Return value from GetLastError
HANDLE hDataBase; // Open handle to the address database
CENOTIFYREQUEST *pRequest; // CENOTIFYREQUEST structure
CEDBASEINFO CEDBInfo; // Structure containing the database data
TCHAR szError[100]; // String to use with error messages

// Allocate memory for pRequest.
pRequest =(CENOTIFYREQUEST *) LocalAlloc(LPTR, sizeof(CENOTIFYREQUEST));
pRequest->dwSize = sizeof(CENOTIFYREQUEST);
pRequest->hwnd = hwndNotify;
pRequest->hHeap = NULL; // Let system allocate memory properly.
pRequest->dwFlags = 0; // Notifications are handled as they were in Windows CE version 1.0.
hDataBase = CeOpenDatabaseEx(
pceguid, // Pointer to the mounted volume
&CeOid, // Location for the database identifier
TEXT("\\Point.cdb"), // Database name
0, // Sort order; 0 indicates to ignore
CEDB_AUTOINCREMENT, // Automatically increase seek pointer
pRequest); // Pointer to a CENOTIFYREQUEST
// structure
if(hDataBase == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
if(dwError == ERROR_NOT_ENOUGH_MEMORY)
{
wsprintf(szError, TEXT("Not enough memory"));
}
else
{
// Possibility of non-existent database; create it.
// Initialize structure CEDBInfo
memset(&CEDBInfo, 0, sizeof(CEDBInfo));

// Create the database with the following specified flags.
// Create the database as uncompressed.
// You can use CeSetDataBaseInfoEx to compress the database.
CEDBInfo.dwFlags =
CEDB_VALIDNAME // szDbaseName is valid
| CEDB_VALIDTYPE // dwDbaseType is valid
| CEDB_VALIDDBFLAGS // HIWORD of dwFlag is valid
| CEDB_VALIDSORTSPEC // rgSortSpecs is valid
| CEDB_NOCOMPRESS; // The database is not compressed.

// Assign the database name as Test.cdb.
wcscpy(CEDBInfo.szDbaseName, TEXT("MyDB"));

// Assign the database type.
CEDBInfo.dwDbaseType = 0;

// Set the number of active sort orders to 4.
// This is the maximum number allowed.
CEDBInfo.wNumSortOrder = 4;

// Initialize the array of sort-order descriptions.
for(index = 0; index < CEDBInfo.wNumSortOrder; ++index)
{
// Sort in descending order.
CEDBInfo.rgSortSpecs[index].dwFlags = CEDB_SORT_DESCENDING;

// Assign the identifier of the properties by which to sort.
// CEDBInfo.rgSortSpecs[index].propid = ...;
}

// Create database "Test.cdb".
CeOid = CeCreateDatabaseEx(pceguid, &CEDBInfo);

if(CeOid == NULL)
{
wsprintf(szError,
TEXT("ERROR:CeCreateDatabaseEx failed(%ld)"),
GetLastError());
}
else // Succeeded in creating the database; open it.
{
hDataBase = CeOpenDatabaseEx(pceguid, &CeOid,
TEXT("MyDB"), 0, 0, pRequest);
}
}
}

(1)如果从台式机上创建一个文件,然后将后缀名改成cdb,那么本条语句返回1,否则总是返回0
(2)如果(1)返回1,那么这条就返回183,如果(1)返回0,这条就返回87.
请各位高手帮忙看看啊,多谢了
...全文
187 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxmman 2010-08-18
  • 打赏
  • 举报
回复
给你个能正确的创建打开数据库的代码吧。能正常工作,刚又试了一次。
//1、挂载数据库卷,如果存在则打开,不存在,就新建一个
if (!CeMountDBVol(&m_ceGuid,DBFILENAME,OPEN_ALWAYS))
{
AfxMessageBox(_T("打开或新建数据卷失败"));
return;
}


//2、接着打开数据库
m_hDB = CeOpenDatabaseEx(&m_ceGuid,&m_ceOid,DBTABLENAME,NULL,CEDB_AUTOINCREMENT,NULL);
if (m_hDB == INVALID_HANDLE_VALUE)
{
//3、 //如果数据库不存在,就新建之
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
CEDBASEINFO ceDbInfo;

ceDbInfo.dwFlags = CEDB_VALIDNAME | CEDB_VALIDTYPE | CEDB_VALIDSORTSPEC ;
wcscpy(ceDbInfo.szDbaseName , DBTABLENAME);
ceDbInfo.dwDbaseType = 0; //第0个
ceDbInfo.wNumSortOrder = 2 ; //排序字段数目

ceDbInfo.rgSortSpecs[0].propid = PID_NO; ///两个排序的方式?
ceDbInfo.rgSortSpecs[0].dwFlags = CEDB_SORT_UNKNOWNFIRST;//CEDB_SORT_CASEINSENSITIVE; //升序,且大小写无关

ceDbInfo.rgSortSpecs[1].propid = PID_NAME;
ceDbInfo.rgSortSpecs[1].dwFlags = CEDB_SORT_UNKNOWNFIRST;//CEDB_SORT_CASEINSENSITIVE; //升序,且大小写无关
m_ceOid = CeCreateDatabaseEx(&m_ceGuid,&ceDbInfo);
if (m_ceOid == 0)
{
AfxMessageBox(_T("创建数据库失败"));
//此处得卸载数据库卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸载数据库文件卷失败"));
}
return ;
}
//4、创建数据库后,应紧接着打开数据库
m_hDB = CeOpenDatabaseEx(&m_ceGuid,&m_ceOid,DBTABLENAME,NULL,CEDB_AUTOINCREMENT,NULL);
if (m_hDB == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("打开数据库失败"));
//此处得卸载数据库卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸载数据库文件卷失败"));
}
return ;
}

}
else
{
AfxMessageBox(_T("打开数据库失败"));
//此处得卸载数据库卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸载数据库文件卷失败"));
}
return ;
}

}
MyLar 2008-08-08
  • 打赏
  • 举报
回复
顶一下
MyLar 2008-08-07
  • 打赏
  • 举报
回复
我用的开发环境是VS2005

19,519

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧