教你使用微软小型数据库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)
&nb
相关新闻>>
- 发表评论
-
- 最新评论 更多>>