C++Builder中的数据模块,类似于C#中的通用数据访问类。可以将连接组件TADOConnection和查询组件TADOQuery放到数据模块中。本质是一个数据库查询的管理类。
(1)创建Data Module类单元
.h文件内容如下:
#ifndef DataModuleUnitH
#define DataModuleUnitH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ADODB.hpp>
#include <DB.hpp>
//---------------------------------------------------------------------------
class TDataModule3 : public TDataModule
{
__published: // IDE-managed Components
TADOConnection *ADOConnection1;
TADOQuery *ADOQuery1;
private: // User declarations
AnsiString m_strIP;
public: // User declarations
__fastcall TDataModule3(TComponent* Owner);
void __fastcall CloseConn();
void __fastcall OpenConnToSQLServer();
void __fastcall OpenConnToAccess();
TADOQuery * __fastcall QueryData(AnsiString strQuery);
int __fastcall ExeSQL(AnsiString strSQL);
TADOQuery * __fastcall SQLStoreProc(AnsiString sql,TParams* param);
};
//---------------------------------------------------------------------------
extern PACKAGE TDataModule3 *DataModule3;
//---------------------------------------------------------------------------
#endif
.cpp文件内容如下:
#include <vcl.h>
#pragma hdrstop
#include "DataModuleUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDataModule3 *DataModule3;
//---------------------------------------------------------------------------
__fastcall TDataModule3::TDataModule3(TComponent* Owner)
: TDataModule(Owner)
{
m_strIP = "129.27.135.68";
ADOQuery1->Connection = ADOConnection1;
}
//---------------------------------------------------------------------------
void __fastcall TDataModule3::CloseConn()
{
ADOConnection1->Close();
}
TADOQuery * __fastcall TDataModule3::QueryData(AnsiString strQuery)
{//执行查询语句
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = strQuery;
ADOQuery1->Open();
return ADOQuery1;
}
int __fastcall TDataModule3::ExeSQL(AnsiString strSQL)
{//执行插入,更新,删除
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = strSQL;
return ADOQuery1->ExecSQL(); //受影响的行数
}
void __fastcall TDataModule3::OpenConnToSQLServer()
{//连接SQLServer
try
{
ADOConnection1->Close();
AnsiString ConnString="Provider=SQLOLEDB.1;\
Persist Security Info=False;User ID=sa;\
Initial Catalog=JZ_GPSDB;\
Data Source="+m_strIP+",1433;PWD=abc2073219";
ADOConnection1->ConnectionString = ConnString;
ADOConnection1->Open();
}
catch(Exception &ex)
{
ShowMessage("数据库连接错误:"+ ex.Message);
}
}
void __fastcall TDataModule3::OpenConnToAccess()
{//连接Access
try
{
ADOConnection1->Close();
AnsiString ConnString="Provider=Microsoft.Jet.OLEDB.4.0;\
Data Source=C:\\用户数据.mdb;Persist Security Info=False";
ADOConnection1->ConnectionString = ConnString;
ADOConnection1->Open();
}
catch(Exception &ex)
{
ShowMessage("数据库连接错误:"+ ex.Message);
}
}
TADOQuery * __fastcall TDataModule3::SQLStoreProc(AnsiString strSql,TParams * param)
{//调用存储过程
ADOQuery1->Active = false;
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(strSql);
for(int i=0; i < param->Count;i++)
{
ADOQuery1->Parameters->Items[i]->Name = param->Items[i]->Name;
ADOQuery1->Parameters->Items[i]->DataType = param->Items[i]->DataType;
ADOQuery1->Parameters->Items[i]->Direction = param->Items[i]->ParamType;
ADOQuery1->Parameters->Items[i]->Value = param->Items[i]->Value;
}
ADOQuery1->Active = true;
return ADOQuery1;
}
(2)在窗口中调用如下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TDataModule3 * pModule = new TDataModule3(0);可声明为成员变量
pModule->OpenConnToAccess();
DataSource1->DataSet = pModule->QueryData("select * from fahuodanwei");
//delete pModule;//注意释放
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{//调用带三个参数的存储过程
TDataModule3 * pModule = new TDataModule3(0);//可声明为成员变量
TParams *param = new TParams();
param->Add();
param->Items[0]->Name = "qtime";//参数名
param->Items[0]->DataType = ftDateTime;//类型
param->Items[0]->ParamType = ptInput;//输入
param->Items[0]->Value = Now();//参数值
param->Add();
param->Items[1]->Name = "sid";
param->Items[1]->DataType = ftString;
param->Items[1]->ParamType = ptInput;
param->Items[1]->Value = "100001";
param->Add();
param->Items[2]->Name = "Remain";
param->Items[2]->DataType = ftInteger;
param->Items[2]->ParamType = ptOutput;
param->Items[2]->Value = 0;
pModule->OpenConnToSQLServer();
DataSource1->DataSet = pModule->SQLStoreProc("exec ups_QueryDataOfSingleDay :qtime,:sid,:Remain output",param);
delete param;
//delete pModule;//注意释放
}
通过数据模块,可以方便实现同一个软件中多种数据库共存,访问不受干扰。