教你使用微软小型数据库esent
#include <esent.h>
#include <Windows.h>
#include <iostream>
#pragma comment (lib, "esent.lib")
using namespace std;
JET_ERR WriteTest(JET_SESID& sessionID,JET_DBID& dbID)
{
JET_TABLEID iTableID;
JET_COLUMNID iColumnId[2];//本例子中表有两列
JET_ERR err = JetOpenTable(sessionID, dbID, "esenttable", NULL, 0, 0, &iTableID);//获取iTableID,插入时需要指定iTableID
if (err!=JET_errSuccess)
{
return err;
}
//以下获取列的ID: iColumnId ***************************************************************
JET_COLUMNDEF columnInfo = {};
columnInfo.cbStruct = sizeof(columnInfo);
err=JetGetColumnInfo(sessionID, dbID, "esenttable", "name", &columnInfo, columnInfo.cbStruct, JET_ColInfo);
if (err!=JET_errSuccess)
{
return err;
}
iColumnId[0] = columnInfo.columnid;//获取iColumnId
ZeroMemory(&columnInfo,sizeof(columnInfo));
columnInfo.cbStruct = sizeof(columnInfo);
err=JetGetColumnInfo(sessionID, dbID, "esenttable", "age", &columnInfo, columnInfo.cbStruct, JET_ColInfo);
if (err!=JET_errSuccess)
{
return err;
}
iColumnId[1] = columnInfo.columnid;//获取iColumnId
//获取iColumnId 完毕***************************************************************
//以下填充要插入的值 表有两列第一列为name 第二列为age
JET_SETCOLUMN *setColumn = new JET_SETCOLUMN[2];
ZeroMemory(setColumn, sizeof(JET_SETCOLUMN)*2);
setColumn[0].columnid = iColumnId[0];//列的id
setColumn[0].pvData = "zhangpeng_linux";//列的值,类型为char*
setColumn[0].cbData = strlen("zhangpeng_linux");//值的长度
setColumn[0].err = JET_errSuccess;
int age=25;
setColumn[1].columnid = iColumnId[1];//列的id
setColumn[1].pvData = (char*)&age;//列的值,类型为char*
setColumn[1].cbData =sizeof(age);//值的长度
setColumn[1].err = JET_errSuccess;
err = JetBeginTransaction(sessionID);
if(JET_errSuccess != err)
{
JetCloseTable(sessionID, iTableID);
delete[] setColumn;
return err;
}
err = JetPrepareUpdate(sessionID, iTableID, JET_prepInsert);
if (JET_errSuccess != err)
{
JetRollback(sessionID, 0);
JetCloseTable(sessionID, iTableID);
delete[] setColumn;
return err;
}
//这里第三个参数为JET_SETCOLUMN结构指针
//第四个参数为第三个参数中JET_SETCOLUMN结构的个数
err = JetSetColumns(sessionID, iTableID, setColumn, 2);
if (JET_errSuccess != err)
{
JetRollback(sessionID, 0);
JetCloseTable(sessionID, iTableID);
delete[] setColumn;
return err;
}
//当函数JetSetColumns成功执行后,不代表每列数据都成功插入,需要对检查 //JET_SETCOLUMN结构中的err字段,确保每一列都成功插入
for (int i = 0; i < 2; i++)
{
if (JET_errSuccess != setColumn[i].err)
{
JetRollback(sessionID, 0);
JetCloseTable(sessionID, iTableID);
delete[] setColumn;
return setColumn[i].err;
}
}
delete[] setColumn;
//后三个字段不需要,填入0即可
err = JetUpdate(sessionID, iTableID, 0, 0, 0);
if (JET_errSuccess != err)
{
JetRollback(sessionID, 0);
JetCloseTable(sessionID, iTableID);
return err;
}
err = JetCommitTransaction(sessionID, 0);
JetCloseTable(sessionID, iTableID);
return err;
}
JET_ERR ReadTest(JET_SESID& sessionID,JET_DBID& dbID)
{
JET_TABLEID iTableID;
JET_COLUMNID iColumnId[2];//本例子中表有两列
JET_ERR err = JetOpenTable(sessionID, dbID, "esenttable", NULL, 0, 0, &iTableID);//获取iTableID
if (err!=JET_errSuccess)
{
return err;
}
//以下获取列的ID: iColumnId
JET_COLUMNDEF columnInfo = {};
columnInfo.cbStruct = sizeof(columnInfo);
err=JetGetColumnInfo(sessionID, dbID, "esenttable", "name", &columnInfo, columnInfo.cbStruct, JET_ColInfo);
if (err!=JET_errSuccess)
{
return err;
}
iColumnId[0] = columnInfo.columnid;//获取iColumnId
ZeroMemory(&columnInfo,sizeof(columnInfo));
columnInfo.cbStruct = sizeof(columnInfo);
err=JetGetColumnInfo(sessionID, dbID, "esenttable", "age", &columnInfo, columnInfo.cbStruct, JET_ColInfo);
if (err!=JET_errSuccess)
{
return err;
}
iColumnId[1] = columnInfo.columnid;//获取iColumnId
char buffer[1024] = { 0 };
int iLen = sizeof(buffer);
int age=-1;
unsigned long nReadBytes=0;
for(err = JetMove(sessionID, iTableID, JET_MoveFirst, 0);JET_errSuccess == err;err = JetMove(sessionID, iTableID, JET_MoveNext, 0))