📄 publicfunction.~pas
字号:
begin
//result:=Tmemorystream.Create ;
AStream := TmemoryStream.Create;
AStream.LoadFromFile(fileName);
if AStream.Size <= 0 then begin Result := false; Abort; end;
if not (ADOTable1.State in [dsinsert, dsedit]) then ADOTable1.edit;
try
with ADOTable1.FieldByName(FiledName) as TBlobField do
begin
Astream.Position := 0;
LoadFromStream(Astream);
end;
result := true;
finally
//ADOTable1.post;
AStream.Position := 0;
//AStream.ReadBuffer(result,Astream.Size-1);
AStream.Free;
end;
end;
//存储文件到blob
//将blob读到tstream
function GetBlobFileToStream(ADOTable1: TAdoQuery; Name: string): TStream;
var
AFiled: Tfield;
begin
Afiled := Tfield.Create(nil);
AFiled.FieldAddress(Name);
AFiled.DataSet := ADOTable1;
result := ADOtable1.CreateBlobStream(AFiled, bmRead);
end;
//将blob读到tstream
function GetBlobToStream(Table: TDataSet; const FieldName: string; var ResultStream: TmemoryStream): Bool;
begin
result := false;
try
with Table.FieldByName(FieldName) as TBlobField do
begin
SaveToStream(ResultStream); //把 tempmemorystream的数据写入 Memorystream当前位置 SaveToStream将自动移动指针等于append
if ResultStream.Size > 0 then
result := true;
end;
finally
//ms.Free;
end;
end;
//返回下一个ID取最大值
function GetNextRecNoMax(ADOConnection: TADOConnection; TableName, Fieldstr, Condition, DesFieldstr: string; FieldLen: integer): longint;
var
lMax: Integer;
lDef: string;
begin
lDef := '000000000000000';
SetLength(lDef, FieldLen - 1);
with TadoQuery.Create(nil) do
try //---- 自动加入编号
begin
Connection := ADOConnection;
SQL.Clear;
SQL.Add('Select max(' + Fieldstr + ') as maxCount from ' + TableName);
Open; //first
if not Eof then //if0 如果此时已到末记录,证明Query1为空
begin
lMax := FieldByName('maxCount').AsInteger;
end
else
lMax := 0; //end_if0 end_else0,已到末记录(Query1为空)直接增一
Close;
end; //end_with
Result := lMax + 1; //最小为1
finally
Free;
end;
end;
//---------------------------------------------------------
//connect with database.
function connect_DB(ADO: TADOConnection; ConnStr: string): bool;
begin
try
if ADO.Connected then
begin
ADO.Close;
end;
ADO.ConnectionString := ConnStr;
ADO.Open;
result := true;
except
begin
MessageDlg('数据库连接失败', mtWarning, [mbYes], 0);
result := false;
exit;
end;
end;
end;
//得到Achar以后的所有值
function GetIDFromChar(ASecStr: string; Achar: string): string;
var
i: integer;
begin
i := pos(Achar, ASecStr);
//showmessage(inttostr(length(achar))); only for test.
//showmessage(inttostr(i));
result := copy(ASecStr, i + 1, Length(ASecStr) - i - 1);
//showmessage(result);
end;
function FormCenter(AForm: TForm): bool;
begin
AForm.Top := (screen.Height - AForm.Height) div 2;
AForm.Left := (screen.Width - AForm.Width) div 2;
end;
function GetPYIndexChar(hzchar: string): char;
begin
case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of
$B0A1..$B0C4: result := 'A';
$B0C5..$B2C0: result := 'B';
$B2C1..$B4ED: result := 'C';
$B4EE..$B6E9: result := 'D';
$B6EA..$B7A1: result := 'E';
$B7A2..$B8C0: result := 'F';
$B8C1..$B9FD: result := 'G';
$B9FE..$BBF6: result := 'H';
$BBF7..$BFA5: result := 'J';
$BFA6..$C0AB: result := 'K';
$C0AC..$C2E7: result := 'L';
$C2E8..$C4C2: result := 'M';
$C4C3..$C5B5: result := 'N';
$C5B6..$C5BD: result := 'O';
$C5BE..$C6D9: result := 'P';
$C6DA..$C8BA: result := 'Q';
$C8BB..$C8F5: result := 'R';
$C8F6..$CBF9: result := 'S';
$CBFA..$CDD9: result := 'T';
$CDDA..$CEF3: result := 'W';
$CEF4..$D188: result := 'X';
$D1B9..$D4D0: result := 'Y';
$D4D1..$D7F9: result := 'Z';
else
result := char(0);
end;
end;
//前导去零
function RemoveFrontZeroFromStr(sec: string): string;
var
i: integer;
begin
for i := 1 to length(sec) do
begin
if sec[i] = '0' then continue;
if sec[i] <> '0' then result := copy(sec, i, length(sec) - i + 1); exit;
end;
end;
//导出到Excel
function ExportToExcel(ADO: TADOConnection; SQLSTR: string): Boolean;
var
xlApp, xlBook, xlSheet, xlQuery: Variant;
adorecordset: TAdoQuery;
begin
{ adoRecordset := TAdoQuery.Create(nil);
adorecordset.Connection:=ado;
adoRecordset.SQL.Clear;
adoRecordset.SQL.Add(SQLSTR);
if Form1.CHKOutTime.Checked then
begin
adoRecordset.parameters.ParamByName('t1').Value:=Form1.DTBegin.DateTime;
adoRecordset.parameters.ParamByName('t2').Value:=Form1.DateTimePicker1.DateTime;
end;
adoRecordset.Open();
xlApp := CreateOleObject('Excel.Application');
xlBook := xlApp.Workbooks.Add;
xlSheet := xlBook.Worksheets['sheet1'];
xlApp.Visible := True;
//把查询结果导入EXCEL数据
xlQuery := xlSheet.QueryTables.Add(adoRecordset.Recordset,xlSheet.Range['A1']); //关键是这一句
xlQuery.FieldNames := True;
xlQuery.RowNumbers := False;
xlQuery.FillAdjacentFormulas := False;
xlQuery.PreserveFormatting := True;
xlQuery.RefreshOnFileOpen := False;
xlQuery.BackgroundQuery := True;
//xlQuery.RefreshStyle := xlInsertDeleteCells;
xlQuery.SavePassword := True;
xlQuery.SaveData := True;
xlQuery.AdjustColumnWidth := True;
xlQuery.RefreshPeriod := 0;
xlQuery.PreserveColumnInfo := True;
xlQuery.FieldNames := True;
xlQuery.Refresh;
adorecordset.Free;}
end;
//导出到Excel
function selectDB(Aform: TForm): string;
var
DBLinkStr: string;
begin
DBLinkStr := PromptDataSource(Aform.Handle, dblinkstr);
result := DBLinkStr;
end;
//get the value from ini file.
function GetCfgValue(const key: string; cfgFileName: string): string;
var
cfn: string;
ini: tinifile;
begin
Result := '';
cfn := ExtractFilePath(Application.ExeName) + cfgFileName;
if FileExists(cfn) then
begin
ini := tinifile.Create(cfn);
try
Result := ini.ReadString('Options', key, Result);
finally
ini.Free;
end;
end;
end;
function SetCfgValue(const key: string; Value: string; cfgFileName: string): bool;
var
cfn: string;
ini: tinifile;
begin
result := true;
cfn := ExtractFilePath(Application.ExeName) + cfgFileName;
if FileExists(cfn) then
begin
ini := tinifile.Create(cfn);
try
ini.WriteString('Options', key, value);
finally
ini.Free;
end;
end;
end;
function GetValueTostr(ATable: string; AFile: string; AFlagField: string; Avalue: string): string;
var
test: string;
begin
// With TAdoQuery.Create(nil) do
result := '';
with TAdoQuery.Create(nil) do
begin
try
connection := stockDM.ADOConn;
Sql.Clear;
if Avalue = 'All' then
begin
test := 'SELECT * FROM ' + ATable + '';
end
else
begin
test := 'SELECT ' + AFile + ' FROM ' + ATable + ' where ' + AFlagField + '='#39 + Avalue + #39'';
end;
sql.Text := test;
open;
if not Isempty then
begin
result := fieldbyname(AFile).AsString;
end
else
begin
result := '';
end;
finally
Free;
end;
end;
end;
//check the valu if exits in a table. exit =true ,else =false
function checkValue(ATable: string; AFile: string; Avalue: string): bool;
var
test: string;
begin
// With TAdoQuery.Create(nil) do
with TAdoQuery.Create(nil) do
begin
try
connection := StockDM.ADOConn;
Sql.Clear;
//henry2007321
//test:='SELECT * FROM '+ATable+' where '+AFile+'='#39 + Avalue +#39'';
test := 'SELECT ' + AFile + ' FROM ' + ATable + ' where ' + AFile + '='#39 + Avalue + #39'';
sql.Text := test;
open;
if not Isempty then
begin
result := true;
end
else
result := false;
finally
Free;
end;
end;
end;
//check values
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -