📄 untfunctions.pas
字号:
end;
end; // with
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.GetId
作者: 马敏钊
日期: 2006.01.11
参数: ItabName, IFieldName: string
返回值: Integer
说明: 获取自动增长的ID号码
-------------------------------------------------------------------------------}
function TDBMrg.GetId(ItabName, IFieldName: string): Integer;
begin
Result := 0;
with GetAnQuery do begin
Close;
SQL.Text := Format('Select Max(%s) as myMax from %s', [IFieldName, ItabName]);
Open;
if FieldByName('MyMax').AsInteger > 0 then
Result := FieldByName('MyMax').AsInteger;
end; // with
inc(Result);
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.GetSomeThing
作者: 马敏钊
日期: 2006.01.11
参数: ItabName, IGetField, IWHereField: string; Ivalue: Variant
返回值: variant
说明: 读取某个字段的值
-------------------------------------------------------------------------------}
function TDBMrg.GetSomeThing(ItabName, IGetField, IWHereField: string;
Ivalue: Variant): variant;
begin
with GetAnQuery(CDb_State_CanUsed) do begin
try
Close;
SQL.Text := Format('Select %s as MyGetField from %s where %s=:VarIant', [IGetField, ItabName, IWHereField]);
Parameters.ParamValues['VarIant'] := Ivalue;
Open;
if RecordCount > 0 then
Result := FieldValues['MyGetField']
else Result := Unassigned;
finally
Close;
end;
end; // with
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.IsExitThis
作者: 马敏钊
日期: 2006.01.11
参数: ItabName, IFieldName: string; Ivalue: Variant
返回值: boolean
说明: 判断是否已经存在这个值
-------------------------------------------------------------------------------}
function TDBMrg.IsExitThis(ItabName, IFieldName: string;
Ivalue: Variant): boolean;
begin
Result := False;
with GetAnQuery(CDb_State_CanUsed) do begin
try
Close;
SQL.Text := Format('Select Count(%s) as MyCount from %s where %s=:variant',
[IFieldName, ItabName, IFieldName]);
Parameters.ParamValues['VarIant'] := Ivalue;
Open;
if Fieldbyname('MyCount').AsInteger > 0 then Result := True;
finally
Close;
end;
end; // with
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.OpenDataset
作者: 马敏钊
日期: 2006.01.11
参数: ISql: string
返回值: TADOQuery
说明: 执行一个查询语句 记得使用完归还(Close)
-------------------------------------------------------------------------------}
function TDBMrg.OpenDataset(ISql: string): TADOQuery;
begin
Result := GetAnQuery;
with Result do begin
Close;
SQL.Text := ISql;
Open;
end; // with
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.OpenDataset
作者: 马敏钊
日期: 2006.01.11
参数: IadoName, ISql: string
返回值: TADOQuery
说明: 用指定的ADO执行
-------------------------------------------------------------------------------}
function TDBMrg.OpenDataset(IadoName, ISql: string): TADOQuery;
begin
Result := GetAnQuery(IadoName);
with Result do begin
Close;
SQL.Text := ISql;
Open;
end; // with
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.PoolCount
作者: 马敏钊
日期: 2006.01.11
参数: 无
返回值: Integer
说明: 查询总共有多少个ADOquery
-------------------------------------------------------------------------------}
function TDBMrg.PoolCount: Integer;
begin
Result := FPool.Count;
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.PoolFreeCount
作者: 马敏钊
日期: 2006.01.11
参数: 无
返回值: Integer
说明: 空闲着的ADO数量
-------------------------------------------------------------------------------}
function TDBMrg.PoolFreeCount: Integer;
var
I: Integer;
begin
Result := 0;
for I := 0 to FPool.Count - 1 do
if (FPool.Objects[i] as TADOQuery).IsEmpty then
Inc(Result);
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.FindDataInDataSet
作者: 马敏钊
日期: 2006.01.11
参数: IData: TDataSet; IFieldName, IFieldValue: string; Iopt: TLocateOptions
返回值: boolean
说明: 在数据集内定位记录
-------------------------------------------------------------------------------}
function TDBMrg.FindDataInDataSet(IData: TDataSet; IFieldName,
IFieldValue: string; Iopt: TLocateOptions): boolean;
var
i, BeginNO: Integer;
LfieldValue, LThenValue: string;
begin
Result := false;
with TADOQuery(IData) do begin
IData.DisableControls;
try
BeginNO := IData.RecNo;
for i := IData.RecNo to IData.RecordCount - 1 do begin // Iterate
LfieldValue := LowerCase(Idata.FieldByName(IFieldName).AsString);
LThenValue := LowerCase(IFieldValue);
if loPartialKey in Iopt then begin
if Pos(LThenValue, LfieldValue) > 0 then begin
Result := True;
Break;
end;
end
else
if CompareText(LThenValue, LfieldValue) = 0 then begin
Result := True;
Break;
end;
IData.Next;
end; // for
if not Result then
Idata.RecNo := BeginNO;
finally
Idata.EnableControls;
end;
end; // with
end;
class function TDBMrg.GetAccessConnStr(IDataSource: string; Ipsd: string = ''):
string;
begin
if Ipsd <> '' then
Result := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security' +
' Info=True;Jet OLEDB:Database Password=%s;', [IDataSource, Ipsd])
else
Result := Format('Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=%s;Mode=Share Deny None;' +
' Extended Properties = ""', [IDataSource]);
end;
{$ENDIF}
class function TDBMrg.GetExcelConnStr(IFileName: string): string;
begin
Result := Format('Provider = Microsoft.Jet.OLEDB.4.0;Data Source ' +
'= %s; Extended Properties = EXCEL 8.0; Persist Security Info = False;', [IFileName]);
end;
class function TDBMrg.GetMsSQLConnStr(IDataSource, IAcc, Ipsd, IDataBase:
string): string;
begin
Result := Format('Provider=SQLOLEDB.1;Password=%s;Persist Security Info=' +
'True;User ID=%s;Initial Catalog=%s;Data Source=%s', [Ipsd, IAcc,
IDataBase, IDataSource]);
end;
class function TDBMrg.GetOracleConnStr(IDataSource, IAcc, Ipsd: string): string;
begin
Result := Format('Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;' +
'User ID=%s;Data Source=%s', [Ipsd, IAcc, IDataSource]);
end;
class function TDBMrg.GetDBFConnStr(IDBPath: string): string;
begin
Result := Format('Provider=MSDASQL.1;Persist Security Info=False; Extended ' +
'Properties="Driver={Microsoft Visual FoxPro Driver};UID=;SourceDB=%s;' +
'SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=PINYIN;Null=Yes;Deleted=no;"',
[IDBPath]);
end;
class function TDBMrg.GetTextConnStr(IDBPath: string): string;
begin
Result := Format('Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=%s' +
';Extended Properties=text', [IDBPath]);
end;
function TDBMrg.GetCount(ItabName: string): Cardinal;
begin
with GetAnQuery(CDb_State_CanUsed) do begin
Close;
SQL.Text := Format('Select Count(*) as MyCount from %s',
[ItabName]);
Open;
Result := Fieldbyname('MyCount').AsInteger;
end; // with
end;
{-------------------------------------------------------------------------------
过程名: TDBMrg.Ready
作者: 马敏钊
日期: 2006.02.21
参数: ItabName:string;Iado:TADOQuery
返回值: 无
说明: 为操作表预备一个ADO
-------------------------------------------------------------------------------}
function TDBMrg.Ready(ItabName: string; Iado: TADOQuery): TADOQuery;
begin
with Iado do begin
Close;
SQL.Text := Format('Select * from %s where 1=2', [ItabName]);
Open;
end; // with
Result := Iado;
end;
function TDBMrg.Ready(ItabName: string; IQueryRight: integer = 1): TADOQuery;
begin
Result := GetAnQuery(IQueryRight);
with TADOQuery(Result) do begin
Close;
SQL.Text := Format('Select * from %s where 1=2', [ItabName]);
Open;
end; // with
end;
function TDBMrg.OpenDataset(IQueryRight: integer; ISql: string; const Args: array
of const): TADOQuery;
begin
ISql := Format(Isql, Args);
Result := GetAnQuery(IQueryRight);
with Result do begin
Close;
SQL.Text := ISql;
Open;
end; // with
end;
function TDBMrg.ExecAnSql(Isql: string;
const Args: array of const): Integer;
begin
Isql := Format(Isql, Args);
with GetAnQuery do begin
try
Close;
SQL.Text := Isql;
Result := ExecSQL;
finally // wrap up
Close;
end; // try/finally
end; // with
end;
{ TCheckThread }
constructor TCheckThread.Create(IsStop: boolean; IDbMrg: TDbmrg);
begin
inherited Create(IsStop);
CheckTime := GetTickCount;
DbMrg := IDbMrg;
FreeOnTerminate := True;
end;
procedure TCheckThread.Execute;
var
I: Integer;
begin
while not Terminated do begin
if GetTickCount - CheckTime < 1000 then
Sleep(100)
else begin
CheckTime := GetTickCount;
with DbMrg.FPool do begin
for I := DbMrg.FPool.Count - 1 downto 0 do begin // Iterate
{如果是可用的就跳过}
if Strings[i] = CDb_State_NoneUsed then
Continue;
if StrToInt(Strings[i]) = CDb_State_EverUsed then
Continue;
{否则倒记时就自动下降1秒}
try
{如果是0就标示为可用}
if Strings[i] = '0' then
Strings[i] := CDb_State_NoneUsed
else
Strings[i] := Format('%d', [StrToInt(Strings[i]) - 1]);
except
Strings[i] := CDb_State_NoneUsed;
end;
end; // for
end; // with
end;
end; // while
end;
function TDBMrg.OpenDataset(ISql: string;
const Args: array of const): TADOQuery;
begin
ISql := Format(Isql, Args);
Result := GetAnQuery;
with Result do begin
Close;
SQL.Text := ISql;
Open;
end; // with
end;
function TDBMrg.OpenDataset(Iado: TADOQuery; ISql: string; const Args: array of
const): TADOQuery;
begin
ISql := Format(Isql, Args);
Result := Iado;
with Result do begin
Close;
SQL.Text := ISql;
Open;
end; // with
end;
function TDBMrg.ExecAnSql(IQueryRight: integer; Isql: string;
const Args: array of const): Integer;
begin
Isql := Format(Isql, Args);
with GetAnQuery(IQueryRight) do begin
try
Close;
SQL.Text := Isql;
Result := ExecSQL;
finally // wrap up
Close;
end; // try/finally
end; // with
end;
class function TDBMrg.GetMySqlConnStr(IDataSource, IDbName, IAcc, Ipsd: string):
string;
begin
Result := Format('DRIVER={MySQL ODBC 3.51 Driver};SERVER=%s;DATABASE=%s;UID=%s;PASSWORD=%s;OPTION=3',
[IDataSource, IDbName, IAcc, Ipsd]);
end;
function TDBMrg.OpenTable(ItabName: string; Iado: TADOQuery): TADOQuery;
begin
with Iado do begin
Close;
SQL.Text := Format('Select * from %s ', [ItabName]);
Open;
end; // with
Result := Iado;
end;
function TDBMrg.CheckModState(IAdo: TADOQuery): boolean;
begin
Result := IAdo.State in [dsEdit, dsinsert];
end;
function TDBMrg.SafePost(Iado: TADOQuery): boolean;
begin
Result := CheckModState(Iado);
if Result then
Iado.Post;
end;
function TDBMrg.OpenTable(ItabName: string; IQueryRight: integer = 1): TADOQuery;
begin
Result := GetAnQuery(IQueryRight);
with TADOQuery(Result) do begin
Close;
SQL.Text := Format('Select * from %s ', [ItabName]);
Open;
end; // with
end;
initialization
finalization
{$IFDEF Db}
//------------------------------------------------------------------------------
// 如果使用了就自动释放
//------------------------------------------------------------------------------
if assigned(Gob_DBMrg) then
Gob_DBMrg.free;
{$ENDIF}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -