DB 問合せ

  HR010.cpp






  
#include "stdafx.h"
#include "resource.h"
#include "MyClass.h"
#include "MyDlg.h"

MyClass App;
MyDlg Dlg;

DEFINE_DLG(Dlg)

WINMAIN(App)

	App.MenuId = IDC_HR010;
	App.IconId = IDI_HR010;
	App.IconIdSmall = IDI_SMALL;

	INIT_DLG(Dlg)

	// DTPicker 使用の為の初期化
	INITCOMMONCONTROLSEX IC;
	IC.dwSize = sizeof( INITCOMMONCONTROLSEX );
	IC.dwICC = ICC_DATE_CLASSES;
	InitCommonControlsEx( &IC );

END_WINMAIN
  



  MyClass.cpp



  
#include "stdafx.h"
#include "resource.h"
#include "MyClass.h"
#include "MyDlg.h"

USE_OBJECT(MyDlg,Dlg)

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

MyClass::MyClass()
{

}

MyClass::~MyClass()
{

}

// *********************************************************
// メインウインドウ作成イベント
// *********************************************************
void MyClass::WMCreate()
{
	Dlg.Open( this, IDD_DIALOG1 );
	this->ReturnValue = -1;
}
  



  MyDlg.h

  
#include "CLClass.h"
#include <LboxDatabase.h>

class MyDlg : public CLDlg  
{
public:
	void GetData();
	void ProcOperator();
	void ProcEnd();
	void WMInitdialog();
	MyDlg();
	virtual ~MyDlg();

	LboxDatabase CurDb;	// データベースオブジェクト(実体型)
	LboxListview *LView;	// リストビューオブジェクト(ポインタ型)
};
  

GetData はオリジナルユーザメンバ関数です
(ProcOperator、ProcEnd、WMInitdialog はオーバーロードされるメンバ関数)



  MyDlg.cpp

  
#include "stdafx.h"
#include "resource.h"
#include "MyDlg.h"

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

MyDlg::MyDlg()
{

}

MyDlg::~MyDlg()
{

}

// *********************************************************
// ダイアログ初期化イベント
// *********************************************************
void MyDlg::WMInitdialog()
{
	this->CenterWindow();

	// ダイアログのプロパティでシステムメニューのチェックボックスを外して下さい
	this->ChangeStyle( WS_MINIMIZEBOX | WS_SYSMENU, 0 );
	this->ChangeExStyle( WS_EX_APPWINDOW, 0 );
	this->ChangeIcon( IDI_HR010 );
	this->StatusCreate( 0 );

	LboxString Buff;

	// MySQL 接続文字列
	Buff.operator = ("MySQL,lightbox,root,");
	CurDb.SetConnectString( 3, &Buff );

	// リストビューインスタンス作成
	LView = new LboxListview( ::GetDlgItem( hDlg, IDC_LIST1 ), 0 );
	LView->Grid();

}

// *********************************************************
// ダイアログ終了処理
// *********************************************************
void MyDlg::ProcEnd()
{
	delete this->LView;
}

// *********************************************************
// オペレータイベント
// *********************************************************
void MyDlg::ProcOperator()
{
	switch( ControlId ) {
		case IDOK:
			GetData();
			break;
	}
}

// *********************************************************
// 問合せ処理
// *********************************************************
void MyDlg::GetData()
{
	if ( !CurDb.Connect() ) {
		MsgOk("接続に失敗しました \n%s", CurDb.ErrMessage.szLboxString);
		return;
	}

	LboxString Query;

	Query.operator = ("select * from 商品分類マスタ");

	int nRet;

	LView->Hide();
	this->StatusSetText( "" );
	nRet = CurDb.LoadSqlData( LView, 0, &Query );
	switch( nRet ) {
		case -1:
			this->StatusSetText( &(CurDb.ErrMessage) );
			break;
		case 0:
			this->StatusSetText( "対象データが存在しません" );
			break;
		default:
			this->StatusPrintf( "%d 件のデータが選択されました", nRet );
			break;
	}
	LView->Fit();
	LView->Show();

	CurDb.DisConnect();	
}
  

その他のデータベース
  
	switch( nType ) {
	case 0:		// Excel
		Buff.operator = ("d:\\temp\\販売管理.xls");
		CurDb.SetConnectString( nType, &Buff );
		break;
	case 1:		// MDB
		Buff.operator = ("d:\\temp\\販売管理.mdb");
		CurDb.SetConnectString( nType, &Buff );
		break;
	case 2:		// SQLServer
		Buff.operator = ("localhost,lightbox2,sa,");
		CurDb.SetConnectString( nType, &Buff );
		break;
	case 3:		// MySQL
		Buff.operator = ("MySQL,lightbox,root,");
		CurDb.SetConnectString( nType, &Buff );
		break;
	case 4:		// Oracle
		Buff.operator = ("ORA,lightbox,lightbox");
		CurDb.SetConnectString( nType, &Buff );
		break;
	case 5:		// PostgreSQL
		Buff.operator = ("PostgreSQL,lightbox,lightbox,");
		CurDb.SetConnectString( nType, &Buff );
		break;
	}
  

3,4,5 は ODBC です
Oracle はその仕様上 DSN,ユーザ(スキーマ),パスワード となります



  条件フィールドの追加

  
// *********************************************************
// 問合せ処理
// *********************************************************
void MyDlg::GetData()
{
	if ( !CurDb.Connect() ) {
		MsgOk("接続に失敗しました \n%s", CurDb.ErrMessage.szLboxString);
		return;
	}

	LboxString Query;

	Query.operator = ("select * from 商品分類マスタ");

	// 以下条件フィールド追加に伴う処理記述
	LboxString Cond( "" );
	LboxString Buff;

	this->EditGetText( IDC_EDIT1, &Buff );
	Buff.Trim( "  " );
	if ( Buff.operator != ( ""  ) ) {
		if ( Cond.operator == ( "" ) ) {
			Cond.operator += (" where ");
		}
		else {
			Cond.operator += (" and ");
		}
		Cond.operator += (" 名称 like ");
		Buff.Enclose( "%" );
		Buff.Enclose( "'" );
		Cond.operator += ( &Buff );
	}

	Query.operator += ( &Cond );
	// 以上条件フィールド追加に伴う処理記述

	int nRet;

	LView->Hide();
	this->StatusSetText( "" );
	nRet = CurDb.LoadSqlData( LView, 0, &Query );
	switch( nRet ) {
		case -1:
			this->StatusSetText( &(CurDb.ErrMessage) );
			break;
		case 0:
			this->StatusSetText( "対象データが存在しません" );
			break;
		default:
			this->StatusPrintf( "%d 件のデータが選択されました", nRet );
			break;
	}
	LView->Fit();
	LView->Show();

	CurDb.DisConnect();	
}
  



  日付条件フィールドの追加

1) エディットコントロール二つとチェックボックス二つを画面に追加

2) MyDlg クラスに以下を追加
  
	LboxDTPicker *From;
	LboxDTPicker *To;
  

3) WMInitdialog に以下を追加
  
	From = new LboxDTPicker(
		this->hDlg,
		this->GetHandle( IDC_EDIT2 ),
		false
	);
	To = new LboxDTPicker(
		this->hDlg,
		this->GetHandle( IDC_EDIT3 ),
		false
	);
	::EnableWindow( From->hWnd, false );
	::EnableWindow( To->hWnd, false );
  

4) ProcEnd に以下を追加
  
	delete From;
	delete To;
  

5) ProcOperator に以下を追加
  
		case IDC_CHECK1:
			if ( this->ButtonIsCheck( IDC_CHECK1 ) ) {
				::EnableWindow( From->hWnd, true );
			}
			else {
				::EnableWindow( From->hWnd, false );
			}
			break;
		case IDC_CHECK2:
			if ( this->ButtonIsCheck( IDC_CHECK2 ) ) {
				::EnableWindow( To->hWnd, true );
			}
			else {
				::EnableWindow( To->hWnd, false );
			}
			break;
  

2) GetData に以下を追加
  
	// 日付条件(開始)
	if ( this->ButtonIsCheck( IDC_CHECK1 ) ) {
		From->GetDateString( &Buff );
		if ( Cond.operator == ( "" ) ) {
			Cond.operator += (" where ");
		}
		else {
			Cond.operator += (" and ");
		}
		Cond.operator += (" 作成日 >= ");
		Buff.operator +=( " 0:00:00" );
		Buff.Enclose( "'" );
		Cond.operator += ( &Buff );
	}
	// 日付条件(終了)
	if ( this->ButtonIsCheck( IDC_CHECK2 ) ) {
		To->GetDateString( &Buff );
		if ( Cond.operator == ( "" ) ) {
			Cond.operator += (" where ");
		}
		else {
			Cond.operator += (" and ");
		}
		Cond.operator += (" 作成日 <= ");
		Buff.operator +=( " 23:59:59" );
		Buff.Enclose( "'" );
		Cond.operator += ( &Buff );
	}
  



  リストビューの編集メニュー

メニューを実装し、メニュー項目を追加して ProcOperator に以下を追加
  
		case IDM_EDIT0:
			LView->CopyClipboard( false, true, 0 );
			break;
		case IDM_EDIT1:
			LView->CopyClipboard( false, true, 1 );
			break;
		case IDM_EDIT3:
			LView->CopyClipboard( false, true, 3 );
			break;
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ