⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lobexampledlg.cpp

📁 oracle 的 blob大字段类型操作例子
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// LOBExampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LOBExample.h"
#include "LOBExampleDlg.h"

#define MAXBUFLEN  204800 //缓冲区长度
//#define MAXBUFLEN  251000 //缓冲区长度

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

FILE *fp,*fp1,*fp2;		//文件指针
ub4 filelen,loblen=0;			//数据文件长度变量
long m_Count;
CProgressCtrl	m_progess;

sb4 callball_write(dvoid *ctxp,dvoid *bufxp,ub4 *lenp, ub1 *piece)
{
	ub4 piecelen;	
	
	//下面变量必须静态变量,用于记录每次读取的数据长度总和
	static ub4 len=MAXBUFLEN;
	
	if((filelen-len)>MAXBUFLEN)
		piecelen=MAXBUFLEN;
	else
		piecelen=filelen-len;
	
	*lenp=piecelen;
	
	if(fread((void*)bufxp,(size_t)piecelen,1,fp)!=1)
	{
		printf("\n读数据文件发生错误!");
		*piece=OCI_LAST_PIECE;
		len=MAXBUFLEN;
		return OCI_CONTINUE;
		
	}
	len+=piecelen;
	m_progess.SetPos((int) len*100.0/filelen);
	
	if(len==filelen)  //最后一块数据
	{
		*piece=OCI_LAST_PIECE;
		len=MAXBUFLEN;
	}
	else
		*piece=OCI_NEXT_PIECE;
	
	return OCI_CONTINUE;
}

//读回调函数
sb4 callback_Read(dvoid *ctxp,CONST dvoid *bufxp,ub4 len,ub1 piece)
{
	static ub4 piece_count=0;
	piece_count++;
	switch(piece)
	{
	case OCI_LAST_PIECE:
		fwrite((void*)bufxp,(size_t)len,1,fp2);
		printf("\n回调函数方式读取%d块数据",piece_count);
		piece_count=0;
		return OCI_CONTINUE;
	case OCI_FIRST_PIECE:
	case OCI_NEXT_PIECE:
		fwrite((void*)bufxp,(size_t)len,1,fp2);
		break;
	default:
		return OCI_ERROR;
	}
	m_progess.SetPos((int) piece_count*len*1.0/loblen*100.0);
	
	return OCI_CONTINUE;

}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLOBExampleDlg dialog

CLOBExampleDlg::CLOBExampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLOBExampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLOBExampleDlg)
	m_strFilename = _T("");
	m_ID = 0;
	m_style = 0;
	m_stroutfilename = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLOBExampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLOBExampleDlg)
	DDX_Text(pDX, IDC_EDIT_BFILENAME, m_strFilename);
	DDX_Text(pDX, IDC_EDIT_ID, m_ID);
	DDX_Control(pDX, IDC_MSFLEXGRID, m_grid);
	DDX_Radio(pDX, IDC_RADIO_POLLING, m_style);
	DDX_Control(pDX, IDC_PROGRESS1, m_progess);
	DDX_Text(pDX, IDC_EDIT_OUTFILENAME, m_stroutfilename);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLOBExampleDlg, CDialog)
	//{{AFX_MSG_MAP(CLOBExampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_READLOB, OnButtonReadlob)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_RADIO_POLLING, OnRadioPolling)
	ON_BN_CLICKED(IDC_RADIO_CALLBACK, OnRadioCallback)
	ON_BN_CLICKED(IDC_BUTTON_OUT, OnButtonOut)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLOBExampleDlg message handlers

BOOL CLOBExampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_progess.SetRange (0,100);
	
	filelen=0;
	m_Count=0;
	nRecordCounts=0;

	
	Init_Grid();//初始化表格
	Init_OCI();//初始化OCI
	GetRecordCounts();//填充数据库表中数据到表格中

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLOBExampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLOBExampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLOBExampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//查询方式LOB写入函数
int CLOBExampleDlg::PollingWriteToLob()
{
	
	ub4 offset=1;loblen=0;
	ub1 bufp[MAXBUFLEN];
	ub4 piecelen,remainder=0,amtp,len=0;
	ub1 piece;
	sword status;
	CString tt;
	amtp=filelen;
	remainder=filelen;
	Error_proc(errhp,OCILobGetLength(svchp,errhp,lob_loc,&loblen));
	//tt.Format ("以查询方式写入前LOB数据长度:%ld",loblen);
//	MessageBox(tt);

	fseek(fp,0,SEEK_SET); //将数据文件指针移到文件头
	if(filelen>MAXBUFLEN)
		piecelen=MAXBUFLEN;
	else
		piecelen=filelen;
	if(fread((void*)bufp,(size_t)piecelen,1,fp)!=1)
	{
		MessageBox("读数据文件发生错误!");
		return -1;		
	}
    
	remainder-=piecelen;
	if(!remainder)  //只有一块数据
	{
		if(status=OCILobWrite(svchp,errhp,lob_loc,&amtp,offset,(dvoid*)bufp,
			(ub4)piecelen,OCI_ONE_PIECE,(dvoid*)0,0,(ub2)0,
			(ub1)SQLCS_IMPLICIT)!=OCI_SUCCESS)
		{
			MessageBox("OCILobWrite()函数执行错误!");
			return -1;		
		}
		else
			m_progess.SetPos(100);
		
	}
	else   //有多块数据
	{
		if(status=OCILobWrite(svchp,errhp,lob_loc,&amtp,offset,(dvoid*)bufp,
			(ub4)MAXBUFLEN,OCI_FIRST_PIECE,(dvoid*)0,0,(ub2)0,
			(ub1)SQLCS_IMPLICIT)!=OCI_NEED_DATA)
		{
			MessageBox("OCILobWrite()函数执行错误!");
			return -1;		
		}
	
		piece=OCI_NEXT_PIECE;
		
		do
		{
			if(remainder>MAXBUFLEN)
				piecelen=MAXBUFLEN;
			else
			{
				piecelen=remainder;
				piece=OCI_LAST_PIECE;
			}
			
			if(fread((void*)bufp,(size_t)piecelen,1,fp)!=1)
			{
				MessageBox("读取文件错误!");
				piece=OCI_LAST_PIECE;
			}
			status=OCILobWrite(svchp,errhp,lob_loc,&amtp,offset,(dvoid*)bufp,
				(ub4)piecelen,piece,(dvoid*)0,0,(ub2)0,(ub1)SQLCS_IMPLICIT);
			remainder-=piecelen;
			m_progess.SetPos((int) ((filelen-remainder)*100.0/filelen));
			
			
		} while(status==OCI_NEED_DATA && !feof(fp));
	}
	
	
    if(status!=OCI_SUCCESS)
		printf("查询方式写入失败!");
	else
	{
		OCILobGetLength(svchp,errhp,lob_loc,&loblen);
		tt.Format ("以查询方式写入后的LOB数据长度:%ld",loblen);
		MessageBox(tt);
		
	}
    return 0;
}

//读取文件到LOB列
void CLOBExampleDlg::OnButtonReadlob() 
{
	BeginWaitCursor(); 

	text *cp=NULL,filename[1024];
	text *selectlocator=(text*)"SELECT lobcol  FROM lobtable2 WHERE id= :1 FOR UPDATE";
	int idcol,callback_error=0,polling_error=0;
    CString tt;
	
	/*
	 
	 //创建OCI环境
	if(status=OCIEnvCreate(&envhp,OCI_DEFAULT,(dvoid *)0, 
		(dvoid* (*)(dvoid*,size_t))0,(dvoid* (*)(dvoid*,dvoid*,size_t))0,
		(void (*)(dvoid *, dvoid *)) 0, (size_t) 0,(dvoid **) 0 ))
	{
		MessageBox("创建OCI环境失败!");
		Error_proc(errhp,status);
		return ;		
	}

	//申请错误句柄
	if(status=OCIHandleAlloc((dvoid*)envhp,(dvoid**)&errhp,(ub4)OCI_HTYPE_ERROR,
		(size_t)0,(dvoid**)0))
	{
		MessageBox("申请错误句柄失败!");
		Error_proc(errhp,status);
		return ;		
	}

	//申请服务器句柄
	if(status=OCIHandleAlloc((dvoid*)envhp,(dvoid**)&srvhp,
		(ub4)OCI_HTYPE_SERVER,(size_t)0,(dvoid**)0))
	{
		MessageBox("申请服务器句柄失败!");
		Error_proc(errhp,status);
		return ;		
	}
	
	//申请服务环境句柄
	if(status=OCIHandleAlloc((dvoid*)envhp,(dvoid**)&svchp,
		(ub4)OCI_HTYPE_SVCCTX,(size_t)0,(dvoid**)0))
	{
		MessageBox("申请服务环境句柄失败!");
		Error_proc(errhp,status);
		return ;		
	}
	
	//申请会话句柄
	if(status=OCIHandleAlloc((dvoid*)envhp,(dvoid**)&authp,
		(ub4)OCI_HTYPE_SESSION,(size_t)0,(dvoid**)0))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -