#pragma once
#include "../stdafx.h"
#include "DBSqlite3.h"
class CDBAccess
{
public:
CDBAccess(void);
~CDBAccess(void);
bool OpenConnection(CString pathname);
int OpenCommandSql(CString sql);
// bool WriteToDB( CString drillname,CString propertyname,char*pBuf,long len);
// bool ReadFromDB(CString drillname,CString propertyname,char*pBuf,long *len);
bool WriteToDB( MDBRecordStruct *tempWellLogCurve);
bool ReadFromDB( MDBRecordStruct *tempWellLogCurves,const int recNum);
bool ReadPropertyNameFromDB( MDBRecordStruct *tempWellLogCurves,const int recNum);
bool UpdateRec( MDBRecordStruct *tempWellLogCurve);
public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
};
// File Name:DBAcess.cpp
// Function:数据库读取
#include "StdAfx.h"
//#include "msado15.h"
#include "DBAccess.h"
#include <vld.h> // 检测内存泄漏
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CDBAccess::CDBAccess(void)
{
}
CDBAccess::~CDBAccess(void)
{
if( m_pRecordset != NULL )
{
m_pRecordset->Close();
m_pRecordset=NULL;
}
if( m_pConnection != NULL )
m_pConnection->Close();
m_pConnection = NULL;
}
bool CDBAccess::OpenConnection(CString pathname)
{
//"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= AdoData.mdb"
CString datasource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ";
datasource+=pathname;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open((_bstr_t)datasource,"","",adModeUnknown);///连接数据库
//上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,
//需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return false;
}
return true;
}
int CDBAccess::OpenCommandSql(CString sql)
{
//读取数据集
int recnum = 0;
try
{
bool m_bSuccess = TRUE;
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
recnum = 0;
while(!m_pRecordset->adoEOF)
{
m_pRecordset->MoveNext();
recnum++;
}
}
catch(_com_error e)///捕捉异常
{
CString tempCString;
tempCString.Format("读取数据库失败:%s",e.ErrorMessage());
AfxMessageBox( tempCString );///显示错误信息
recnum = -1;
}
return recnum;
}
//bool CDBAccess::ReadFromDB(CString drillname,CString propertyname,char*pBuf,long *len)
bool CDBAccess::ReadFromDB( MDBRecordStruct *tempWellLogCurves,const int recNum)
{
int i;
m_pRecordset->MoveFirst();
FieldsPtr tempFieldsPtr = m_pRecordset->GetFields();
//FieldPtr tempFieldPtr = NULL;
//CString tempFieldName;
int fieldnum = tempFieldsPtr->GetCount();
//for(i=0 ; i<fieldnum ; i++ )
MDBRecordStruct *tempWellLogCurve = tempWellLogCurves;
for(i=0 ; i<recNum ; i++)
{ // m_pRecordset->adoEOF() )
tempWellLogCurve->drillname = tempFieldsPtr->GetItem("DrillName")->GetValue();
tempWellLogCurve->wellLogIndex = tempFieldsPtr->GetItem("WellLogIndex")->GetValue();
tempWellLogCurve->logMark = tempFieldsPtr->GetItem("LogMark")->GetValue();
tempWellLogCurve->propertyName = tempFieldsPtr->GetItem("PropertyName")->GetValue();
tempWellLogCurve->filterRarefyMark = tempFieldsPtr->GetItem("FilterRarefyMark")->GetValue();
tempWellLogCurve->method = tempFieldsPtr->GetItem("Method")->GetValue();
tempWellLogCurve->parameter1 = tempFieldsPtr->GetItem("Parameter1")->GetValue();
tempWellLogCurve->parameter2 = tempFieldsPtr->GetItem("Parameter2")->GetValue();
tempWellLogCurve->filterMethodMarkBeforeRare = tempFieldsPtr->GetItem("FilterMethodMarkBeforeRare")->GetValue();
tempWellLogCurve->filterParamBeforeRare = tempFieldsPtr->GetItem("FilterParamBeforRare")->GetValue();
tempWellLogCurve->maxValue = tempFieldsPtr->GetItem("MaxValue")->GetValue();
tempWellLogCurve->minValue = tempFieldsPtr->GetItem("MinValue")->GetValue();
tempWellLogCurve->weightAverageValue = tempFieldsPtr->GetItem("WeightAverageValue")->GetValue();
tempWellLogCurve->pointNum = tempFieldsPtr->GetItem("PointNum")->GetValue(); // 获取 PointNum
tempWellLogCurve->len = tempFieldsPtr->GetItem( "Data")->ActualSize; ///得到数据的长度
tempWellLogCurve->pBuf = new char[tempWellLogCurve->len];
char *buf = NULL;
if( tempWellLogCurve->len > 0)
{
_variant_t varBLOB;
varBLOB = tempFieldsPtr->GetItem( "Data")->GetChunk(tempWellLogCurve->len);
if(varBLOB.vt == (VT_ARRAY | VT_UI1)) ///判断数据类型是否正确
{
SafeArrayAccessData(varBLOB.parray,(void **)&buf); ///得到指向数据的指针
// ----- 在这里我们可以对pBuf中的数据进行处理 -----
memcpy(tempWellLogCurve->pBuf,buf,tempWellLogCurve->len);
SafeArrayUnaccessData (varBLOB.parray);
}
}
m_pRecordset->MoveNext();
tempWellLogCurve++;
}
return true;
}
// --------- 只读取属性名称 ---------
bool CDBAccess::ReadPropertyNameFromDB( MDBRecordStruct *tempWellLogCurves,const int recNum)
{
int i;
m_pRecordset->MoveFirst();
FieldsPtr tempFieldsPtr = m_pRecordset->GetFields();
//FieldPtr tempFieldPtr = NULL;
//CString tempFieldName;
int fieldnum = tempFieldsPtr->GetCount();
//for(i=0 ; i<fieldnum ; i++ )
MDBRecordStruct *tempWellLogCurve = tempWellLogCurves;
for(i=0 ; i<recNum ; i++)
{
tempWellLogCurve->propertyName = tempFieldsPtr->GetItem("PropertyName")->GetValue();
tempWellLogCurve->minValue = tempFieldsPtr->GetItem("MinValue")->GetValue();
tempWellLogCurve->maxValue = tempFieldsPtr->GetItem("MaxValue")->GetValue();
m_pRecordset->MoveNext();
tempWellLogCurve++;
}
return true;
}
//bool CDBAccess::WriteToDB(CString drillname,CString propertyname, char*pBuf,long len)
bool CDBAccess::WriteToDB( MDBRecordStruct *tempWellLogCurve)// CString drillname,int index,int logMark,CString propertyname,int filterRarefyMark,CString method,float parameter1,float parameter2,int pointnum,char*pBuf,long len)
{
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
long i,len;
char *pBuf = NULL;
//m_pRecordset->raw_MoveLast();
i = m_pRecordset->AddNew(); ///添加新记录
//if(i>0)
{
m_pRecordset->PutCollect(TEXT("DrillName"), _variant_t(tempWellLogCurve->drillname));
m_pRecordset->PutCollect(TEXT("WellLogIndex"), _variant_t(tempWellLogCurve->wellLogIndex));
m_pRecordset->PutCollect(TEXT("LogMark"), _variant_t(tempWellLogCurve->logMark));
m_pRecordset->PutCollect(TEXT("PropertyName"), _variant_t(tempWellLogCurve->propertyName));
m_pRecordset->PutCollect(TEXT("FilterRarefyMark"),_variant_t(tempWellLogCurve->filterRarefyMark));
m_pRecordset->PutCollect(TEXT("Method"), _variant_t(tempWellLogCurve->method));
m_pRecordset->PutCollect(TEXT("FilterMethodMarkBeforeRare"),_variant_t(tempWellLogCurve->filterMethodMarkBeforeRare));
m_pRecordset->PutCollect(TEXT( "FilterParamBeforRare"),_variant_t(tempWellLogCurve->filterParamBeforeRare));
m_pRecordset->PutCollect(TEXT("Parameter1"), _variant_t(tempWellLogCurve->parameter1));
m_pRecordset->PutCollect(TEXT("Parameter2"), _variant_t(tempWellLogCurve->parameter2));
m_pRecordset->PutCollect(TEXT("MaxValue"), _variant_t(tempWellLogCurve->maxValue));
m_pRecordset->PutCollect(TEXT("MinValue"), _variant_t(tempWellLogCurve->minValue));
m_pRecordset->PutCollect(TEXT("WeightAverageValue"),_variant_t(tempWellLogCurve->weightAverageValue));
m_pRecordset->PutCollect(TEXT("PointNum"), _variant_t(tempWellLogCurve->pointNum));
if(tempWellLogCurve->pBuf)
{
rgsabound[0].lLbound = 0;
len = rgsabound[0].cElements = tempWellLogCurve->len;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); ///创建SAFEARRAY对象
pBuf = tempWellLogCurve->pBuf;
for( i = 0 ; i < len ; i++ )
SafeArrayPutElement (psa, &i, pBuf++); ///将pBuf指向的二进制数据保存到SAFEARRAY对象psa中
varBLOB.vt = VT_ARRAY | VT_UI1; ///将varBLOB的类型设置为BYTE类型的数组
varBLOB.parray = psa; ///为varBLOB变量赋值
m_pRecordset->GetFields()->GetItem("Data")->AppendChunk(varBLOB);///加入BLOB类型的数据
}
m_pRecordset-> Update(); ///保存我们的数据到库中
}
return true;
}
bool CDBAccess::UpdateRec(MDBRecordStruct *tempWellLogCurve)
{
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
long i,len;
char *pBuf = NULL;
m_pRecordset->MoveFirst();
FieldsPtr tempFieldsPtr = m_pRecordset->GetFields();
FieldPtr tempFieldPtr = NULL;
int fieldnum = tempFieldsPtr->GetCount();
// ------- -------
//m_pRecordset->PutCollect(TEXT("DrillName"), _variant_t(tempWellLogCurve->drillname));
//m_pRecordset->PutCollect(TEXT("WellLogIndex"), _variant_t(tempWellLogCurve->wellLogIndex));
//m_pRecordset->PutCollect(TEXT("LogMark"), _variant_t(tempWellLogCurve->logMark));
//m_pRecordset->PutCollect(TEXT("PropertyName"), _variant_t(tempWellLogCurve->propertyName));
m_pRecordset->PutCollect(TEXT("FilterRarefyMark"),_variant_t(tempWellLogCurve->filterRarefyMark));
m_pRecordset->PutCollect(TEXT("Method"), _variant_t(tempWellLogCurve->method));
m_pRecordset->PutCollect(TEXT("FilterMethodMarkBeforeRare"),_variant_t(tempWellLogCurve->filterMethodMarkBeforeRare));
m_pRecordset->PutCollect(TEXT("FilterParamBeforRare"),_variant_t(tempWellLogCurve->filterParamBeforeRare));
m_pRecordset->PutCollect(TEXT("Parameter1"), _variant_t(tempWellLogCurve->parameter1));
m_pRecordset->PutCollect(TEXT("Parameter2"), _variant_t(tempWellLogCurve->parameter2));
m_pRecordset->PutCollect(TEXT("MaxValue"), _variant_t(tempWellLogCurve->maxValue));
m_pRecordset->PutCollect(TEXT("MinValue"), _variant_t(tempWellLogCurve->minValue));
m_pRecordset->PutCollect(TEXT("WeightAverageValue"),_variant_t(tempWellLogCurve->weightAverageValue));
m_pRecordset->PutCollect(TEXT("PointNum"), _variant_t(tempWellLogCurve->pointNum));
if(tempWellLogCurve->pBuf)
{
rgsabound[0].lLbound = 0;
len = rgsabound[0].cElements = tempWellLogCurve->len;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); ///创建SAFEARRAY对象
pBuf = tempWellLogCurve->pBuf;
for( i = 0 ; i < len ; i++ )
SafeArrayPutElement (psa, &i, pBuf++); ///将pBuf指向的二进制数据保存到SAFEARRAY对象psa中
varBLOB.vt = VT_ARRAY | VT_UI1; ///将varBLOB的类型设置为BYTE类型的数组
varBLOB.parray = psa; ///为varBLOB变量赋值
tempFieldPtr = tempFieldsPtr->GetItem("Data");
tempFieldPtr->AppendChunk(varBLOB);
// m_pRecordset->GetFields()->GetItem("Data")->AppendChunk(varBLOB);///加入BLOB类型的数据
}
m_pRecordset-> Update(); ///保存我们的数据到库中
return true;
}
其中涉及到的结构体定义
struct MDBRecordStruct
{
CString drillname; // 井名,当 logMark = 5时为 地层名称
int wellLogIndex; // 测录井数据序号(WellLogs数组中的序号);当 logMark = 5 时,0:测井组,1:录井组
int logMark; // 0:测井数据,1:录井数据, 2:井迹三维曲线,3:测井采样点空间位置曲线,4:录井采样点空间位置曲线, 5:细层采样点、控制点数组, 6:钻井所属的不同曲线的控制点要素数据(LQY 2018-6-14)
CString propertyName; // 属性(曲线)名称,
int filterRarefyMark; // 0:原始曲线,1:滤波曲线,2:抽稀曲线, 当 logMark = 5时为地层序号
int method; // 处理方法:0:未做处理, 当 filterRarefyMark==1 时(滤波) 1:中值滤波、2:Kalman滤波,...; 当 filterRarefyMark==2 时(抽稀),1:douglas精简、2:间隔采样),当 logMark = 5时为地层块体序号
int filterMethodMarkBeforeRare; // 抽稀前的滤波处理方法(-1.原始曲线或滤波曲线,0:对原始曲线抽稀,1:对中值滤波曲线抽稀,2:对Kalman滤波曲线抽稀,3);当 logMark = 5 时,为细层序号
int filterParamBeforeRare; // 抽稀前的滤波处理参数(-1:原始曲线或滤波曲线,当 filterRarefyMark==1 时(滤波) 1:中值滤波、2:Kalman滤波,...; 当 filterRarefyMark==2 时(抽稀),1:douglas精简、2:间隔采样, 当 logMark=5 时,控制点个数
float parameter1; // 处理参数1,如中值滤波的样点个数,douglas精简的阈值, 无值取-1 , 当 logMark = 5时为 分组合并的半径 delta(在该半径的点加权平均作为一个点)
float parameter2; // 处理参数2,备用,无值取-1
//float parameter1BeforeRare; // 抽稀前的处理参数1(-1:原始曲线或滤波曲线
//float parameter2BeforeRare; // 抽稀前的处理参数2(-1:原始曲线或滤波曲线
float maxValue; // 属性最大值
float minValue; // 属性最小值
float weightAverageValue; // 属性加权平均值
int pointNum; // 样点数, ,当 logMark = 5时为采样点个数
char *pBuf; // 二进制大对象
long len; // 大对象的字节数
public:
MDBRecordStruct()
{
drillname = "";
wellLogIndex = 0;
logMark = 0;
propertyName = "";
filterRarefyMark = 0;
method = 0;
parameter1 = 0.0F;
parameter2 = 0.0F;
filterMethodMarkBeforeRare = -1;
filterParamBeforeRare = -1;
/* parameter1BeforeRare = 0.0F;
parameter2BeforeRare = 0.0F;*/
maxValue = 0.0F;
minValue = 0.0F;
weightAverageValue = 0.0F;
pointNum = 0;
pBuf = NULL;
len = 0;
};
~MDBRecordStruct()
{
if(pBuf != NULL)
{
delete []pBuf;
pBuf = NULL;
}
};
};