//-------------------------------------------------------------------
// VORecordset implementation
//-------------------------------------------------------------------
//
// Copyright �00 Virtual Office Systems Incorporated
// All Rights Reserved
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This code can be compiled, modified and distributed freely, providing
// that this copyright information remains intact in the distribution.
//
// This code may be compiled in original or modified form in any private
// or commercial application.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage, in any form, caused
// by this code. Use it at your own risk.
//-------------------------------------------------------------------
/*-----------------------------------------------------------------------------------------------
**-----------------------------------------------------------------------------------------------
**说明:此代码的最初资源来自于网络
**
**类功能:Sql CE3.1数据集操作相关功能
**
**
**修改日期:2008-07-30
**修改人员:***:
***:修改了得到记录集Open()方法的参数传递类型;
修改了GetFidleValue()方法的参数类型和参数个数;
**备注:
**-----------------------------------------------------------------------------------------------
**-----------------------------------------------------------------------------------------------
**
**----修改记录----
**
**修改日期:2008.08.07
**修改内容:修改了类的Close()和析构函数,并在构造函数中对成员变量m_bIsOpen进行了初始化
修改了记录集的产生方式,以便可以返回记录数
**-----------------------------------------------------------------------------------------------
**-----------------------------------------------------------------------------------------------*/
#include "stdafx.h"
#include "VORecordset.h"
#include <string.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#ifndef TRACE
#define TRACE(s) OutputDebugString(s)
#endif
#else
#ifndef TRACE
#define TRACE(s)
#endif
#endif
const IID IID__Recordset = { 0x113033f6, 0xf682, 0x11d2, { 0xbb, 0x62, 0x00, 0xc0, 0x4f, 0x68, 0x0a, 0xcc}};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVORecordset::CVORecordset(CVOConnection& rConn) : m_rConn(rConn), m_rsRecordSet(NULL)
{
m_ProgID = _T("ADOCE.Recordset.3.1");
m_bInit = FALSE;
m_bIsOpen = FALSE;
if(!m_bInit)
{
Initialize();
}
}
CVORecordset::~CVORecordset()
{
//AfxMessageBox(L"int REcordSet Destruction!");
if (IsOpen())
{
Close();
}
if(m_rsRecordSet)
m_rsRecordSet->Release();
}
//初始化数据记录集
BOOL CVORecordset::Initialize()
{
HRESULT hr;
// m_dlg = NULL;
hr = CLSIDFromProgID( m_ProgID, &m_ClsID);
hr = CoCreateInstance(m_ClsID, NULL, CLSCTX_INPROC_SERVER, IID__Recordset, (LPVOID*)&m_rsRecordSet);
// VARIANT varConn;
// If Connection object is using CEDB instead of a .CDB filename, use it
// as a persistent connection. For .CDB filenames, each recordset Open()
// requires its own connection unfortunately.
if((_Connection*)m_rConn)
{
m_ActiveConn.pdispVal = (_Connection*)m_rConn;
m_ActiveConn.vt = VT_DISPATCH;
hr = m_rsRecordSet->put_ActiveConnection(m_ActiveConn);
}
if(FAILED(hr))
{
MessageBox(NULL,_T("数据集连接创建失败!"),_T("警告"),MB_OK|MB_ICONWARNING);
return FALSE;
}
return(!FAILED(hr));
}
//执行Sql语句
BOOL CVORecordset::Open(LPCTSTR szCommand)
{
HRESULT hr;
VARIANT varSql;
VariantInit(&varSql);
varSql.vt = VT_BSTR;
varSql.bstrVal = (LPTSTR)szCommand;
hr = m_rsRecordSet->Open(varSql, m_ActiveConn, adOpenKeyset, adLockPessimistic,adCmdText);
m_bIsOpen = (!FAILED(hr));
return m_bIsOpen;
}
//关闭记录集
BOOL CVORecordset::Close()
{
//AfxMessageBox(L"In RecordSet Close()");
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::Close() RecordSet COM Object not initialized\n"));
return FALSE;
}
if(IsOpen())
{
m_rsRecordSet->Close();
//m_rsRecordSet->Release();
}
m_bIsOpen = FALSE;
return TRUE;
}
//查看是否到记录集的头部
BOOL CVORecordset::IsBOF()
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::BOF() RecordSet COM Object not initialized\n"));
return FALSE;
}
VARIANT_BOOL bValue;
HRESULT hr = m_rsRecordSet->get_BOF(&bValue);
return (bValue == VARIANT_TRUE);
}
//查看是否到记录集的未端
BOOL CVORecordset::IsEOF()
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::EOF() RecordSet COM Object not initialized\n"));
return FALSE;
}
VARIANT_BOOL bValue;
HRESULT hr = S_OK;
hr = m_rsRecordSet->get_EOF(&bValue);
return (bValue == VARIANT_TRUE);
}
//移动记录集,到第一个的位置
BOOL CVORecordset::MoveFirst()
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::MoveFirst() RecordSet COM Object not initialized\n"));
return FALSE;
}
HRESULT hr = m_rsRecordSet->MoveFirst();
return (!FAILED(hr));
}
//移动记录集,到下条记录
BOOL CVORecordset::MoveNext()
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::MoveNext() RecordSet COM Object not initialized\n"));
return FALSE;
}
HRESULT hr = m_rsRecordSet->MoveNext();
return (!FAILED(hr));
}
//获取数据记录相应记录
BOOL CVORecordset::GetFieldValue(int iField, CString &strField)
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::GetFieldValue() RecordSet COM Object not initialized\n"));
//return _variant_t(0L);
return FALSE;
}
VARIANT varRecValue;
HRESULT hr;
VARIANT varData,varFieldSelect;
long lFieldsCount=0;
int i = 0;
VariantInit(&varRecValue);
VariantInit(&varData);
VariantInit(&varFieldSelect);
if(m_bIsOpen)
{
hr = m_rsRecordSet->get_Fields(&m_Fields);
int aas = GetLastError();
hr = m_Fields->get_Count(&lFieldsCount);
int aad = GetLastError();
if( iField < (int)lFieldsCount)
{
varFieldSelect.vt = VT_UI2;
varFieldSelect.uiVal = iField;
hr = m_Fields->get_Item(varFieldSelect, &m_Field);
hr = m_Field->get_Value(&varData);
}
}
switch(varData.vt)
{
case VT_BSTR:
strField = varData.bstrVal;
break;
case VT_I4:
strField.Format(_T("%d"), varData.lVal);
break;
case VT_I2:
strField.Format(L"%d", varData.iVal);
break;
case VT_I8:
strField.Format(L"%d", varData.lVal);
case VT_INT:
strField.Format(L"%d", varData.intVal);
break;
case VT_DECIMAL:
strField.Format(L"%f", varData.decVal);
break;
case VT_R4:
strField.Format(L"%f", varData.fltVal);
break;
case VT_R8:
strField.Format(L"%lf", varData.dblVal);
break;
case VT_I1:
strField.Format(L"%c", varData.cVal);
break;
case VT_BOOL:
strField.Format(L"%d", varData.boolVal);
break;
case VT_DATE:
strField.Format(_T("%s"), varData.date);
break;
default:
strField = (LPCTSTR)_bstr_t(varData);
break;
}
return (!FAILED(hr));
}
////本操作执行未成功,请注意,如果想更改数据请用SQL语句完成
BOOL CVORecordset::SetFieldValue(int iField, VARIANT value)
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::SetFieldValue() RecordSet COM Object not initialized\n"));
return FALSE;
}
BOOL bReturn=FALSE;
// VARIANT varRecValue;
HRESULT hr;
VARIANT varFieldSelect,varEmpty;//,varData;
long lFieldsCount=0;
int i=0;
// VariantInit(&varRecValue);
// VariantInit(&varData);
VariantInit(&varEmpty);
VariantInit(&varFieldSelect);
hr = m_rsRecordSet->get_Fields(&m_Fields);
hr=m_Fields->get_Count
山的那边还是山~
- 粉丝: 26
- 资源: 10
最新资源
- 同济大学作业之-LPC分析(男声变女声)和PCM编码
- java超市订单管理系统源码数据库 MySQL源码类型 WebForm
- 记录windows安装nvm:nvm-setup-2024-11-16.exe.zip
- 同济大学数字信号处理实验(包含实验报告)
- Kettle 是Kettle E.T.T.L. Envirnonment只取首字母的缩写,这意味着它被设计用来帮助你实现你的
- java微信小程序B2C商城 H5+APP源码 前后端分离数据库 MySQL源码类型 WebForm
- matplotlib 绘制随机漫步图
- java版快速开发框架后台管理系统源码数据库 MySQL源码类型 WebForm
- Java实现植物大战僵尸简易版
- matplotlib 绘制随机漫步图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈