DELPHI 数据库操作类(工具类)
时间:2021-07-01 10:21:17
帮助过:6人阅读
UnDm;
interface
uses
SysUtils, Classes, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, Menus, StdCtrls,
ADODB,
DB,inifiles;
type
TRecordResult =
(RRError, RRZero, RROK);
TDataStatus =
(DsSel, DsEdit, DsAdd);
type
TDataConnection =
class
DataAdoConn: TADOConnection;
qryTemp1: TADOQuery;
qryTemp2: TADOQuery;
qryTemp3: TADOQuery;
qryTemp4: TADOQuery;
private
public
constructor Create;
overload;
//连接数据库
function DbConn: Boolean;
//启动事物
function AdoBeginTrans: Boolean;
//回滚事物
function AdoRollbackTrans: Boolean;
//提交事物
function AdoCommitTrans: Boolean;
//
Function GetOpenQuery(
const sSql, sTitle: String; _AdoQry: TADOQuery; Is_View: Boolean = True): TRecordResult;
overload;
function UpdateSql(
const sSql, sTitle: String; IsView: Boolean=
True): TRecordResult;
function GetAllUser: Integer;
end;
implementation
uses Querypa;
constructor TDataConnection.
Create;
var
I:Integer;
begin
inherited;
DataAdoConn:=TADOConnection.
Create(
nil);
qryTemp1:=TADOQuery.
Create(
nil);
qryTemp2:=TADOQuery.
Create(
nil);
qryTemp3:=TADOQuery.
Create(
nil);
qryTemp4:=TADOQuery.
Create(
nil);
DataAdoConn.LoginPrompt:=
False;
qryTemp1.Connection:=
DataAdoConn;
qryTemp2.Connection:=
DataAdoConn;
qryTemp3.Connection:=
DataAdoConn;
qryTemp4.Connection:=
DataAdoConn;
end;
function TDataConnection.DbConn: Boolean;
const
//sDbConn = ‘Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;User ID=%s;Data Source=%s‘;
sDbConn =
‘Provider=OraOLEDB.Oracle.1;Password=%s;User ID=%s;Data Source=%s;Persist Security Info=True‘;
var
sUdlFile: String;
ServerIni:TiniFile;
SOURCE,USER,PASSWORD:string;
sSql:string;
begin
//获得数据库连接串
ServerIni := TIniFile.
Create(ExtractFilePath(Application.ExeName) +
‘ServerInfor.Ini‘);
SOURCE := ServerIni.ReadString(
‘HIS‘,
‘SOURCE‘,
‘0‘);
USER := ServerIni.ReadString(
‘HIS‘,
‘USER‘,
‘0‘);
PASSWORD := ServerIni.ReadString(
‘HIS‘,
‘PASSWORD‘,
‘0‘);
sSql :=
Format(sDbConn, [PASSWORD, USER, SOURCE]);
Result :=
False;
if DataAdoConn.Connected
then
DataAdoConn.Close;
//连接数据库
DataAdoConn.ConnectionString :=sSql;
//‘FILE NAME=‘+sUdlFile;
Try
DataAdoConn.Open;
if not DataAdoConn.Connected
then
begin
Result :=
false;
Exit;
end
else
begin
Result :=
true;
end;
Except
Result :=
False;
Exit;
end;
end;
function TDataConnection.AdoBeginTrans: Boolean;
Var
nResult: Integer;
begin
Result :=
False;
Try
nResult :=
DataAdoConn.BeginTrans;
if nResult >
0 then
Result :=
True;
except
On E: Exception do
Begin
SaveError(‘启动事务‘, E.Message);
end;
end;
end;
function TDataConnection.AdoCommitTrans: Boolean;
Var
nResult: Integer;
begin
Result :=
False;
Try
DataAdoConn.CommitTrans;
Result :=
True;
except
On E: Exception do
Begin
SaveError(‘提交事务‘, E.Message);
end;
end;
end;
function TDataConnection.AdoRollbackTrans: Boolean;
Var
nResult: Integer;
begin
Result :=
False;
Try
DataAdoConn.RollbackTrans;
Result:True;
except
On E: Exception do
Begin
SaveError(‘回滚事务‘, E.Message);
end;
end;
end;
/// <summary>
/// 更新数据库涵数 插入或修改
/// sSql SQL语句
/// sTitle执行标题
/// IsView出错后是否弹出消息
/// </summary>
function TDataConnection.UpdateSql(
const sSql, sTitle: String;
IsView: Boolean): TRecordResult;
Var
nResult: Integer;
AdoQry:TADOQuery;
begin
AdoQry:=TADOQuery.
Create(
nil);
AdoQry.Connection:=
DataAdoConn;
Result :=
RRError;
With AdoQry do
Begin
Close;
SQL.Text :=
sSql;
Try
nResult :=
ExecSQL;
if nResult =
0 then
Result :=
RRZero
else
Result :=
RROK;
Except
on E: Exception do
Begin
SaveError(sTitle, sSql+ #
10#
13 +
E.Message);
MsgShow(E.Message, 3);
Exit;
end;
end;
end;
if Result = RRZero
then
Begin
SaveError(sTitle, sSql+ #
10#
13 +
‘执行没有返回正确结果!‘);
if IsView
then
MsgShow(‘执行没有返回正确结果!‘,
0);
Exit;
end;
end;
/// <summary>
/// 查询数据库语句
/// sSql SQL语句
/// sTitle执行标题
/// _AdoQry Adoquery控件
/// IsView出错后是否弹出消息
/// </summary>
function TDataConnection.GetOpenQuery(
const sSql, sTitle: String; _AdoQry: TADOQuery;
Is_View: Boolean): TRecordResult;
begin
Result :=
RRError;
With _AdoQry do
Begin
Close;
SQL.Text :=
sSql;
Try
Open;
if IsEmpty
then
Result :=
RRZero;
if Not IsEmpty
then
Result :=
RROK;
Except
on E: Exception do
Begin
SaveError(sTitle, sSql+ #
10#
13 +
E.Message);
MsgShow(E.Message, 3);
Exit;
end;
end;
end;
if (Result = RRZero)
and Is_View
then
Begin
SaveError(sTitle, sSql+ #
10#
13 +
sTitle);
MsgShow(sTitle, 3);
end;
end;
function TDataConnection.GetAllUser: Integer;
begin
with qryTemp1
do
begin
Close;
SQL.Text :=
‘SELECT USERID, name From T_USERS‘;
Open;
end;
Result :=
qryTemp1.RecordCount;
end;
end.
————————————————
声明
var
dm: TDataConnection;
创建
//首选连接服务器数据库
dm:=TDataConnection.Create;
if not dm.DbConn then
begin
MsgShow(‘服务器数据库连接失败,无法启动服务‘,1);
dm.Free;
Exit;
end;
执行SQL语句
strSQL:=‘ UPDATE A SET AA=‘‘1‘‘‘;
DM.UpdateSql(strSQL,‘更新数据库‘,false);
查询数据库
strSQL:=‘ SELECT * FROM A‘;
DM.GetOpenQuery(strSQL,‘更新数据库‘,ADOQUERY1,false);
事务就添加TRY后在里面增加事务就可以
DELPHI 数据库操作类(工具类)
标签:down erro 服务器 lin 执行sql user web query ons