面向过程的ado建造者模式
{*******************************************************}
{ }
{ ADO数据工厂 }
{ }
{ 版权所有 (C) 2008 咏南工作室 }
{ }
{*******************************************************}
unit uDataFactory;
interface
uses
SysUtils,ADODB,DB,Classes,uCommFunc,ActiveX;
{ 生成连接对象 }
function GetADOConn(Owner:TComponent;AProvider:Integer=1):TADOConnection;
{ 生成数据集对象 }
function GetADODataSet(Owner:TComponent;AConn:TADOConnection;ASQL:string):TDataSet;
{ 执行事务性SQL }
procedure UpdateADODataSet(Owner:TComponent;AConn:TADOConnection;ASQL:string);
{ 生成DataSource对象 }
function GetDataSource(Owner:TComponent):TDataSource;
{ 数据绑定 }
procedure LinkControl(ADataSet:TDataSet;ADataSource:TDataSource);
implementation
function GetDataSource(Owner:TComponent):TDataSource;
var
FDataSource:TDataSource;
begin
FDataSource:=TDataSource.Create(Owner);
Result:=FDataSource;
end;
procedure LinkControl(ADataSet:TDataSet;ADataSource:TDataSource);
begin
ADataSource.DataSet:=ADataSet;
end;
//==============================================================================
// AProvider=1 sql Server: 'sqloledb'
// AProvider=2 Access: 'Microsoft.Jet.OLEDB.4.0'
//==============================================================================
function GetADOConn(Owner:TComponent;AProvider:Integer=1):TADOConnection;
var
Conn:TADOConnection;
sFileName,sSection:string;
begin
sFileName:=GetINIFile;
Conn:=TADOConnection.Create(Owner);
with Conn do
begin
LoginPrompt:=False;
if AProvider=1 then
begin
Provider:='sqloledb';
sSection:='sql server';
Properties['Data Source'].Value:=ReadIniFile(sFileName,sSection,'server','');
Properties['User ID'].Value:=ReadIniFile(sFileName,sSection,'userid','');
Properties['Password'].Value:=ReadIniFile(sFileName,sSection,'password','');
Properties['Initial Catalog'].Value:=ReadIniFile(sFileName,sSection,'database','');
end;
if aprovider=2 then
begin
Provider:='microsoft.jet.oledb.4.0';
sSection:='access';
Properties['Jet OLEDB:Database Password'].Value:=ReadIniFile(sFileName,sSection,'database password','');
Properties['Data Source'].Value:=ReadIniFile(sFileName,sSection,'server','');
Properties['User ID'].Value:=ReadIniFile(sFileName,sSection,'userid','');
Properties['Password'].Value:=ReadIniFile(sFileName,sSection,'password','');
end;
try
open;
Result:=Conn;
except
raise Exception.Create('数据库连接失败');
end;
end;
end;
function GetADODataSet(Owner:TComponent;AConn:TADOConnection;ASQL:string):TDataSet;
var
DataSet:TADODataSet;
begin
dataset:=TADODataSet.Create(owner);
dataset.Connection:=aconn;
with dataset do
begin
Close;
CommandText:='';
CommandText:=ASQL;
Open;
end;
Result:=DataSet;
end;
procedure UpdateADODataSet(Owner:TComponent;AConn:TADOConnection;ASQL:string);
var
FDataSet:TADOCommand;
begin
FDataSet:=TADOCommand.Create(Owner);
FDataSet.Connection:=AConn;
AConn.BeginTrans;
try
with FDataSet do
begin
CommandText:='';
CommandText:=ASQL;
Execute;
end;
AConn.CommitTrans;
except
AConn.RollbackTrans;
end;
end;
end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/archive/2008/03/14/2941061.html