unit ADO;
{This unit provides a quick access into ADO
It handles all it's own exceptions
It assumes it is working with SQL Server, on a PLC Database
If an exception is thrown with a [PLCErr] suffix:
the suffix is removed, and ErrMsg is set to the remaining string
otherwise
the whole exception is reported in ErrMsg
Either way, the function call fails.
Globals: adocn - connection which all other ADO objects use
adors - Recordset
adocmd - Command Object
adocmdprm - Command Object set aside for Parametric querying
ConnectionString
- Connection String used for connecting
ErrMsg - Last Error Message
ADOActive - Indicator as to whether ADO has been started yet
Functions:
General ADO
ADOStart:Boolean;
ADOReset:Boolean;
ADOStop:Boolean;
Recordsets
RSOpen(SQL:string;adRSType,adLockType,adCmdType:integer;UseServer:Boolean):Boolean;
RSClose:Boolean;
Normal Command Procedures
CMDExec(SQL:string;adCmdType:integer):Boolean;
Parametric Procedures
PRMClear:Boolean;
PRMSetSP(StoredProcedure:string;WithClear:Boolean):Boolean;
PRMAdd(ParamName:string;ParamType,ParamIO,ParamSize:integer;Val:variant):Boolean;
PRMSetParamVal(ParamName:string;val:variant):Boolean;
PRMGetParamVal(ParamName:string;var val:variant):Boolean;
Field Operations
function SQLStr(str:string;SQLStrType:TSQLStrType);
function SentenceCase(str:string):string;
--to convert from 'FIELD_NAME' -> 'Field Name' call
SQLStr(SentenceCase(txt),ssFromSQL);
}interfaceuses OLEAuto, sysutils;
const{Param Data Types}
adInteger = 3;
adSingle = 4;
adDate = 7;
adBoolean = 11;
adTinyInt = 16;
adUnsignedTinyInt = 17;
adDateTime = 135;
advarChar = 200;
{Param Directions}
adParamInput = 1;
adParamOutput = 2;
adParamReturnValue = 4;
{Command Types}
adCmdText = 1;
adCmdTable = 2;
adCmdStoredProc = 4;
adCmdTableDirect = 512;
adCmdFile = 256;
{Cursor/RS Types}
adOpenForwardOnly = 0;
adOpenKeyset = 1;
adOpenDynamic = 2;
adOpenStatic = 3;
{Lock Types}
adLockReadOnly = 1;
adLockOptimistic = 3;
{Cursor Locations}
adUseServer = 2;
adUseClient = 3;
function ADOReset: Boolean;
function ADOStop: Boolean;
function RSOpen(SQL: string; adRSType, adLockType, adCmdType: integer;
UseServer: Boolean): Boolean;
function RSClose: Boolean;
function CMDExec(SQL: string; adCmdType: integer): Boolean;
function PRMClear: Boolean;
function PRMSetSP(StoredProcedure: string; WithClear: Boolean): Boolean;
function PRMAdd(ParamName: string; ParamType, ParamIO, ParamSize: integer; Val:
variant): Boolean;
function PRMSetParamVal(ParamName: string; val: variant): Boolean;
function PRMGetParamVal(ParamName: string; var val: variant): Boolean;
type
TSQLStrType = (ssToSQL, ssFromSQL);
function SQLStr(str: string; SQLStrType: TSQLStrType): string;
function SentenceCase(str: string): string;
var
adocn, adors, adocmd, adocmdPrm: variant;
ConnectionString, ErrMsg: string;
ADOActive: boolean = false;
implementationvar
UsingConnection: Boolean;
function ADOStart: Boolean;
begin//Get the Object Referencestry
adocn := CreateOLEObject('ADODB.Connection');
adors := CreateOLEObject('ADODB.Recordset');
adocmd := CreateOLEObject('ADODB.Command');
adocmdprm := CreateOLEObject('ADODB.Command');
result := true;
excepton E: Exception dobegin
ErrMsg := e.message;
Result := false;
end;
end;
ADOActive := result;
end;
function ADOReset: Boolean;
begin
Result := false;
//Ensure a clean slate...ifnot (ADOStop) then
exit;
//Restart all the ADO Referencesifnot (ADOStart) then
exit;
//Wire up the Connections//If the ADOconnetion fails, all objects will use the connection string// directly - poorer performance, but it works!!try
adocn.ConnectionString := ConnectionString;
adocn.open;
adors.activeconnection := adocn;
adocmd.activeconnection := adocn;
adocmdprm.activeconnection := adocn;
UsingConnection := true;
excepttry
adocn := unassigned;
UsingConnection := false;
adocmd.activeconnection := ConnectionString;
adocmdprm.activeconnection := ConnectionString;
excepton e: exception dobegin
ErrMsg := e.message;
exit;
end;
end;
end;
Result := true;
end;
function ADOStop: Boolean;
begintryifnot (varisempty(adocn)) thenbegin
adocn.close;
adocn := unassigned;
end;
adors := unassigned;
adocmd := unassigned;
adocmdprm := unassigned;
result := true;
excepton E: Exception dobegin
ErrMsg := e.message;
Result := false;
end;
end;
ADOActive := false;
end;
function RSOpen(SQL: string; adRSType, adLockType, adCmdType: integer;
UseServer: Boolean): Boolean;
begin
result := false;
//Have two attempts at getting the required Recordsetif UsingConnection thenbegintryif UseServer then
adors.CursorLocation := adUseServer
else
adors.CursorLocation := adUseClient;
adors.open(SQL, , adRSType, adLockType, adCmdType);
exceptifnot (ADOReset) then
exit;
tryif UseServer then
adors.CursorLocation := adUseServer
else
adors.CursorLocation := adUseClient;
adors.open(SQL, , adRSType, adLockType, adCmdType);
excepton E: Exception dobegin
ErrMsg := e.message;
exit;
end;
end;
end;
endelsebegin//Use the Connetcion String to establish a linktry
adors.open(SQL, ConnectionString, adRSType, adLockType, adCmdType);
exceptifnot (ADOReset) then
exit;
try
adors.open(SQL, ConnectionString, adRSType, adLockType, adCmdType);
excepton E: Exception dobegin
ErrMsg := e.message;
exit;
end;
end;
end;
end;
Result := true;
end;
function RSClose: Boolean;
begintry
adors.Close;
result := true;
excepton E: Exception dobegin
ErrMsg := e.message;
result := false;
end;
end;
end;
function CMDExec(SQL: string; adCmdType: integer): Boolean;
begin
result := false;
//Have two attempts at the execution..try
adocmd.commandtext := SQL;
adocmd.commandtype := adCmdType;
adocmd.execute;
excepttryifnot (ADOReset) then
exit;
adocmd.commandtext := SQL;
adocmd.commandtype := adCmdType;
adocmd.execute;
excepton e: exception dobegin
ErrMsg := e.message;
exit;
end;
end;
end;
result := true;
end;
function PRMClear: Boolean;
var
i: integer;
begintryfor i := 0 to (adocmdprm.parameters.count) - 1 dobegin
adocmdprm.parameters.delete(0);
end;
result := true;
excepton e: exception dobegin
ErrMsg := e.message;
result := false;
end;
end;
end;
function PRMSetSP(StoredProcedure: string; WithClear: Boolean): Boolean;
begin
result := false;
//Have two attempts at setting the Stored Procedure...try
adocmdprm.commandtype := adcmdStoredProc;
adocmdprm.commandtext := StoredProcedure;
if WithClear thenifnot (PRMClear) then
exit;
result := true;
excepttryifnot (ADOReset) then
exit;
adocmdprm.commandtype := adcmdStoredProc;
adocmdprm.commandtext := StoredProcedure;
//NB: No need to clear the parameters, as an ADOReset will have done this..
result := true;
excepton e: exception dobegin
ErrMsg := e.message;
end;
end;
end;
end;
function PRMAdd(ParamName: string; ParamType, ParamIO, ParamSize: integer; Val:
variant): Boolean;
var
DerivedParamSize: integer;
begin//Only try once to add the parameter (a call to ADOReset would reset EVERYTHING!!)trycase ParamType of
adInteger: DerivedParamSize := 4;
adSingle: DerivedParamSize := 4;
adDate: DerivedParamSize := 8;
adBoolean: DerivedParamSize := 1;
adTinyInt: DerivedParamSize := 1;
adUnsignedTinyInt: DerivedParamSize := 1;
adDateTime: DerivedParamSize := 8;
advarChar: DerivedParamSize := ParamSize;
end;
adocmdprm.parameters.append(adoCmdPrm.createparameter(ParamName, ParamType,
ParamIO, DerivedParamSize, Val));
excepton e: exception dobegin
ErrMsg := e.message;
end;
end;
end;
function PRMSetParamVal(ParamName: string; val: variant): Boolean;
begin//Only try once to set the parameter (a call to ADOReset would reset EVERYTHING!!)try
adocmdprm.Parameters[ParamName].Value := val;
result := true;
excepton e: exception dobegin
ErrMsg := e.message;
result := false;
end;
end;
end;
function PRMGetParamVal(ParamName: string; var val: variant): Boolean;
begin//Only try once to read the parameter (a call to ADOReset would reset EVERYTHING!!)try
val := adocmdprm.Parameters[ParamName].Value;
result := true;
excepton e: exception dobegin
ErrMsg := e.message;
result := false;
end;
end;
end;
function SQLStr(str: string; SQLStrType: TSQLStrType): string;
var
FindChar, ReplaceChar: char;
begin{Convert ' '->'_' for ssToSQL (remove spaces)
Convert '_'->' ' for ssFromSQL (remove underscores)}case SQLStrType of
ssToSQL:
begin
FindChar := ' ';
ReplaceChar := '_';
end;
ssFromSQL:
begin
FindChar := '_';
ReplaceChar := ' ';
end;
end;
result := str;
while Pos(FindChar, result) > 0 do
Result[Pos(FindChar, result)] := ReplaceChar;
end;
function SentenceCase(str: string): string;
var
tmp: char;
i {,len}: integer;
NewWord: boolean;
begin
NewWord := true;
result := str;
for i := 1 to Length(str) dobeginif (result[i] = ' ') or (result[i] = '_') then
NewWord := trueelsebegin
tmp := result[i];
if NewWord thenbegin
NewWord := false;
result[i] := chr(ord(result[i]) or 64); //Set bit 6 - makes uppercaseendelse
result[i] := chr(ord(result[i]) and 191); //reset bit 6 - makes lowercaseend;
end;
{This was the original way of doing it, but I wanted to look for spaces or '_'s,
and it all seemed problematic - if I find a better way another day, I'll alter the above...
if str<>'' then
begin
tmp:=LowerCase(str);
len:=length(tmp);
tmp:=Uppercase(copy(tmp,1,1))+copy(tmp,2,len);
i:=pos('_',tmp);
while i<>0 do
begin
tmp:=copy(tmp,1,i-1)+' '+Uppercase(copy(tmp,i+1,1))+copy(tmp,i+2,len-i);
i:=pos('_',tmp);
end;
end;
result:=tmp;}end;
end.
Эта единица Delphi, называемая ADO, предназначена для быстрого доступа к компонентам ADO (ActiveX Data Objects) для работы с базами данных. Единица содержит несколько функций и процедур для управления соединениями ADO, записями, командами и параметрами.
Глобальные переменные
adocn: объект соединения
adors: объект записи
adocmd: объект команды
adocmdprm: объект команды для параметрических запросов
ConnectionString: строка, представляющая строку подключения к базе данных
ErrMsg: строка для хранения сообщений об ошибках
ADOActive: булевый индикатор, указывающий, запущены ли ADO или нет
Функции
ADOStart: инициализирует компоненты ADO и настраивает соединение
ADOReset: сбрасывает компоненты ADO, включая соединение, запись и объект команды
ADOStop: останавливает компоненты ADO и освобождает ресурсы
RSOpen: открывает запись с указанным запросом SQL и настройками
RSClose: закрывает открытую запись
CMDExec: выполняет объект команды с указанным запросом SQL и настройками
PRMClear: очищает все параметры из объекта команды
PRMSetSP: устанавливает хранимую процедуру для объекта команды
PRMAdd: добавляет параметр в объект команды
PRMSetParamVal: устанавливает значение параметра в объекте команды
PRMGetParamVal: получает значение параметра из объекта команды
Другое
SQLStr: конвертирует строку из/в формат SQL (например, заменяет пробелы на подчерки)
SentenceCase: конвертирует строку в формат предложения (то есть, капитализирует первую букву и оставляет остальную часть в нижнем регистре)
В целом, эта единица обеспечивает удобный способ взаимодействия с базами данных с помощью ADO в Delphi. Однако пожалуйста, обратите внимание, что некоторые код может быть устаревшим или несовместимым с новыми версиями Delphi.
Быстрый доступ к ADO: утилита для быстрого и простого взаимодействия с базами данных SQL Server на платформе Delphi.
Комментарии и вопросы
Получайте свежие новости и обновления по Object Pascal, Delphi и Lazarus прямо в свой смартфон. Подпишитесь на наш Telegram-канал delphi_kansoftware и будьте в курсе последних тенденций в разработке под Linux, Windows, Android и iOS
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.