|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2009-2-20 22:37:27
|
显示全部楼层
{--------------------------------------------------------------}
{ 本地数据操作函数 }
{ Version:1.0 }
{ 作者:knight }
{--------------------------------------------------------------}
unit myData;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
IniFiles,StdCtrls,ADODB,Db,registry,DBClient,
SConnect,uFindPath;
///1、access数据库操作
function ADOCn(var conn:TADOConnection; ///连接ACCESS数据库
DBPath:string;
PWD:string='';
UID:string=''):boolean;stdcall;overload;
function AdoCn(var TSCn:TSocketConnection; ///局域网分布式数据库连接
DBpath:string;
pwd:string='';
uid:string=''):boolean;overload;
function ADORS(var cn:TADOConnection; ///执行SQL语句
var RS:TADOQuery;
SQLStr:string;
KeepConnect:boolean=false):boolean;stdcall;overload;
function AdoRs(var TSCn:TSocketConnection; ///局域网分布式数据库SQL执行
ClientSet:TClientDataSet;
SQL:string;
KeepConnect:boolean=false):boolean;overload;
///2、ini 文件操作
function readinifile(FilePath,Sectionstr,KeyStr:string; ///读 ini 文件
mode:integer=0):variant; stdcall;
function writeinifile(FilePath,Sectionstr,KeyStr:string; ///写 ini 文件
values:variant;
mode:integer=0):boolean; stdcall;
procedure DelKey(FilePath,Sectionstr,KeyStr:string); ///删除KEY
procedure DelSection(filepath,sectionstr:string); ///删除 段
function GetKeyList(filepath,sectionstr:string):tstrings;///获取所有KEY
function GetSectionList(filepath:string):tstrings; ///获取所有 段
function GetLn(filepath,sectionstr:string):tstrings; /// 获取段下的所有行
//3、txt文件操作
function WritetxtFile(Filepath:shortstring; ///写文本文件
S:string;
OverWrite:boolean=false):boolean;
function Readtxtfile(filepath:shortstring):string; ///读文本文件
///4、注册表操作
Function ReadReg(Root:hkey;path,key:string):string; ///读注册表
Function WriteReg(Root:hkey;
path,key,val:string;
mode:byte=0):boolean; ///写注册表
var
ServerName: string;
ProviderName: string;
dbport: integer;
IsLanData: boolean;
//data1,data2,data3: dbinfor;
LanHostIP: string;
implementation
///1、access数据库操作
///=================================================
/// Access的 ADO 连接
///=================================================
function ADOCn(var Conn:TADOConnection;
DBPath:string;
PWD:string='';
UID:string=''):boolean;stdcall;overload;
var
dbname:string;
begin
result:=false;
dbname:='';
if FindFilePath(dbname,dbpath) then
begin
with conn do
begin
if Connected=true then Connected:=false ;
ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;'+
'Data Source='+dbname+';'+
'Persist Security Info=false;'+
'Jet OLEDB:Database Password='+PWD+';'+
'User ID='+UID+';mode=3';
Try
KeepConnection:=True;
LoginPrompt:=false;
Open;
Result:=true;
Except
close;
Result:=false;
End;
end;
end
else begin conn.Close; result:=false; end;
end;
function AdoCn(var TSCn:TSocketConnection;
DBpath:string;
pwd:string='';
uid:string=''):boolean;overload; //局域网分布式数据库连接
begin
result:=false;
TSCn.LoginPrompt:=false;
TSCn.Connected:=false;
if LanHostIP='' then LanHostIP:='127.0.0.1';
TSCn.Address:=LanHostIP;
if dbport<2 then dbport:=211;
TSCn.Port:=dbport;
if ServerName='' then ServerName:='SQLServer.SQLSvr';//com服务名
TSCn.ServerName:=ServerName;
try
TSCn.Connected:=true;
try
TSCn.AppServer.SetDBName(dbpath,PWD,uid);
result:=TSCn.Connected;
except result:=false; end;
except result:=false; end;
end;
///=================================================
/// Access的数据 SQL 语句操作
///=================================================
function ADORS(var cn:TADOConnection;
var RS:TADOQuery;
SQLStr:string;
KeepConnect:boolean=false):boolean;stdcall;overload;
begin
result:=false;
with rs do
begin
Connection:=cn;
SQL.Text:=SQLStr;
Try
if KeepConnect then open//返回结果
else begin
ExecSQL; //不返回结果
close; cn.Close;
end;
Result:=true;
Except
close;
cn.Close;
Result:=false;
End;
end;
end;
function AdoRS(var TSCn:TSocketConnection;
ClientSet:TClientDataSet;
SQL:string;
KeepConnect:boolean=false):boolean;overload; //局域网分布式数据库SQL执行
begin
result:=false;
clientset.Active:=false;
clientset.RemoteServer:=TSCn;
if ProviderName='' then ProviderName:='DataSetProvider1'; //数据打包传输控件名
clientset.ProviderName:=ProviderName;
try
TSCn.AppServer.SQLExecute(sql,'knight','qishiq',keepconnect);
clientset.Active:=KeepConnect;
result:=true;
except
result:=false;
end;
end;
///2、ini 文件操作
///=================================================
/// 写 ini 文件
///=================================================
function writeinifile(FilePath:string;
Sectionstr:string;
KeyStr:string;
values:variant;
mode:integer=0):boolean;stdcall;
var
myinifile:tinifile;
filename:string;
begin
result:=false;
findfilepath(filename,filepath);
try
myinifile:=TInifile.Create(filename);
except
exit;
end;
case mode of
1:myinifile.writeinteger(SectionStr,KeyStr,Values);
2:myinifile.writebool(SectionStr,KeyStr,Values);
else
myinifile.writestring(SectionStr,KeyStr,Values);
end;
myinifile.Destroy;
result:=true;
end;
///=================================================
/// 删除 KEY
///=================================================
procedure DelKey(FilePath:string;
Sectionstr:string;
KeyStr:string);
var
myinifile:tinifile;
filename:string;
begin
filename:=FilePath;
myinifile:=TInifile.Create(filename);
myinifile.DeleteKey(Sectionstr,Keystr);
myinifile.Destroy;
end;
///=================================================
/// 删除 小节
///=================================================
procedure DelSection(filepath,sectionstr:string);
var
myinifile:tinifile;
filename:string;
begin
filename:=FilePath;
myinifile:=TInifile.Create(filename);
myinifile.EraseSection(Sectionstr);
myinifile.Destroy
end;
///=================================================
/// 读 ini 文件
///=================================================
function readinifile(FilePath:string;
Sectionstr:string;
KeyStr:string;
mode:integer=0):variant; stdcall;
var
myinifile:tinifile;
filename:string;
ReValue:variant;
begin
if not findfilepath(filename,filepath) then
begin
case mode of
1:Result:=0;
2:Result:=false;
else
Result:='';
end;
exit;
end;
myinifile:=TInifile.Create(filename);
case mode of
1:ReValue:=myinifile.Readinteger(Sectionstr,Keystr,0);
2:ReValue:=myinifile.Readbool(Sectionstr,Keystr,False);
else
ReValue:=myinifile.Readstring(Sectionstr,Keystr,'');
end;
myinifile.Destroy;
Result:=ReValue;
end;
///=================================================
/// 读一小节中的 所有关键字名
///=================================================
function GetKeyList(filepath,sectionstr:string):TStrings;
var
myinifile:tinifile;
filename:string;
st:tstrings;
begin
st:=tstringlist.Create;
filename:=FilePath;
myinifile:=TInifile.Create(filename);
myinifile.readsection(Sectionstr,st);
result:=st;
myinifile.Destroy
end;
///=================================================
/// 读一小节的 所有行(包括关键字、=、值)
///=================================================
function GetLn(filepath,sectionstr:string):TStrings;
var
myinifile:tinifile;
filename:string;
st:tstrings;
begin
st:=tstringlist.Create;
filename:=FilePath;
myinifile:=TInifile.Create(filename);
myinifile.readsectionvalues(sectionstr,st);
result:=st;
myinifile.Destroy
end;
///=================================================
/// 读INI文件中 所有小节名
///=================================================
function GetSectionList(filepath:string):tstrings;
var
myinifile:tinifile;
filename:string;
st:tstrings;
begin
st:=tstringlist.Create;
filename:=FilePath;
myinifile:=TInifile.Create(filename);
myinifile.readsections(st);
result:=st;
myinifile.Destroy
end;
///3、txt文件操作
///=================================================
/// 写 txt文本文件
/// 文档如果已经存在 则打开
/// 如果不存在 则建立一个新的文档
///=================================================
function WritetxtFile(FilePath:shortstring;S:string;OverWrite:boolean=false):boolean;
var
FileHandle: Integer;
Buffer: PChar;
wrLen: word;
st: tstrings;
i: integer;
wstr: string;
filename: string;
begin
result:=false;
wstr:='';
if findfilepath(filename,filepath) then
begin
if OverWrite then
begin
try
deletefile(filename);
except
EXIT;
end;
wstr:=s;
FileHandle := FileCreate(FileName);
end ELSE BEGIN
st:=tstringlist.Create;
st.LoadFromFile(filename);
st.Add(s);
for i:=0 to st.Count-1 do
begin
wstr:=wstr+st+char(13)+char(10);
end;
freeandnil(st);
FileHandle := FileOpen(FileName, fmOpenReadWrite);
END;
end else begin
WSTR:=S;
FileHandle := FileCreate(FileName);
end;
if FileHandle < 0 then
Begin
Exit;
End;
WrLen:=length(wstr);
GetMem(Buffer,WrLen+1); ///分配一个 ?BYTE 的内存空间
try
StrPCopy(Buffer, wstr); ///把文本拷贝到内存中
FileWrite(FileHandle,Buffer^,WrLen);///把内存中的前 WrLen 个 BYTE 内容写进打开的文件中
finally
FreeMem(Buffer);
end;
FileClose(FileHandle);
result:=true;
end;
///=================================================
/// 读文本 很简单
///=================================================
function Readtxtfile(filePath:shortstring):string;
var
ss:tstrings;
s:string;
i:integer;
filename:string;
begin
result:='';
s:='';
if not findfilepath(filename,filepath) then exit;
ss:=tstringlist.Create;
ss.LoadFromFile(filename); //uses Classes.pas
for i:=0 to ss.Count-1 do
s:=s+ss+char(13)+char(10);
ss.Free;
result:=s;
end ;
///4、注册表操作
///=================================================
/// 读 注册表
///=================================================
Function ReadReg(Root:hkey;path,key:string):string;
var
reg:Tregistry;
begin
result:='';
reg:=Tregistry.Create;
reg.RootKey:=Root;
if not reg.OpenKeyReadOnly(path) then
begin reg.CloseKey; exit; end;
result:=reg.ReadString(key);
reg.CloseKey;
reg.Free;
end;
///=================================================
/// 写 注册表
///=================================================
Function writereg(Root:hkey;path,key,val:string;mode:byte=0):boolean;
var
reg:Tregistry; //当key='' 时 表示写入默认值
begin
result:=false;
reg:=Tregistry.Create;
reg.RootKey:=Root;
if not reg.OpenKey(path,true) then
begin
reg.CloseKey; exit;
end;
if mode=0 then
begin
reg.WriteString(key,val);
end
else if mode=1 then
begin
reg.WriteInteger(key,strtointdef(val,0));
end
else if mode=2 then
begin
reg.WriteFloat(key,strtofloatdef(val,0.0));
end
else if mode=3 then
begin
reg.WriteBool(key,strtobooldef(val,false));
end;
result:=true;
reg.CloseKey;
reg.Free;
end;
end. |
|