📄 mainunit.pas
字号:
unit MainUnit;
interface
uses
Windows, DB, ADODB, SysUtils, ActiveX;
type
PInt = ^Integer;
PPInt = ^pInt;
PPChar = ^PChar;
PByte = ^Byte;
PWORD = ^WORD;
PDWORD = ^DWORD;
PPointer = ^Pointer;
PDouble = ^Double;
PInt64 = ^Int64;
function StartDB(szDBServer: PChar; bKeepConnect: Integer): Integer; cdecl;
function SelectCommand(szSql: PChar; pRetBuf: PPointer): Integer; cdecl;
function ExcuteCommand(szSql: PChar): Integer; cdecl;
function CloseDB(): Integer; cdecl;
function FreeAndNilMemory(p: PPointer): Integer; cdecl;
var
gConnection : TAdoConnection = nil;
implementation
function StartDB(szDBServer: PChar; bKeepConnect: Integer): Integer;
begin
try
CoInitialize(nil);
gConnection := TADOConnection.Create(nil);
if not Assigned(gConnection) then begin
Result := -1;
Exit;
end;
gConnection.ConnectionString := Format('Provider=SQLOLEDB.1;Password=szmobile;Persist Security Info=True;User ID=szmobile;Initial Catalog=SZMOBILEPROJ;Data Source=%s', [szDBServer]);
gConnection.LoginPrompt := False;
if (bKeepConnect = 1) then
gConnection.KeepConnection := True
else
gConnection.KeepConnection := False;
gConnection.Connected := True;
Result := 0;
except
Result := -1;
end;
end;
function SelectCommand(szSql: PChar; pRetBuf: PPointer): Integer;
var
AdoQuery : TADOQuery;
//guid : TGUID;
nStructSize, i, j : Integer;
nRow, nPos : Integer;
dwPos, dwStart : DWORD;
chTmp : Char;
nInt64 : Int64;
//szTmp : String;
begin
AdoQuery := nil;
try
AdoQuery := TADOQuery.Create(nil);
if not Assigned(AdoQuery) then begin
Result := -1;
Exit;
end;
//CreateGUID(guid);
//szTmp := IntToStr(guid.D1) + IntToStr(guid.D2) + IntToStr(guid.D3);
//AdoQuery.Name := 'ADOQry_' + szTmp;
AdoQuery.Connection := gConnection;
AdoQuery.Prepared := True;
AdoQuery.Close;
AdoQuery.SQL.Text := szSql;
AdoQuery.Open();
if (AdoQuery.RecordCount < 1) then
begin
Result := 0;
AdoQuery.Destroy;
Exit;
end
else
begin
nStructSize := 0;
for i := 0 to AdoQuery.FieldCount -1 do begin
if (AdoQuery.Fields[i].DataType = ftString) then
nStructSize := nStructSize + AdoQuery.Fields[i].DataSize -1
else if (AdoQuery.Fields[i].DataType = ftWord) then //(在Delphi中取出的tinyint类型为word,因此特殊处理)
nStructSize := nStructSize + AdoQuery.Fields[i].DataSize -1
else
nStructSize := nStructSize + AdoQuery.Fields[i].DataSize;
end;
pRetBuf^ := GetMemory(AdoQuery.RecordCount * nStructSize +10);
FillMemory(pRetBuf^, AdoQuery.RecordCount * nStructSize +10, 0);
if (pRetBuf^ = nil) then begin
Result := -1;
AdoQuery.Destroy;
Exit;
end;
dwStart := DWORD(pRetBuf^);
nRow := 0;
while not AdoQuery.Eof do begin
dwPos := nRow * nStructSize;
for i := 0 to AdoQuery.Fields.Count -1 do begin
case AdoQuery.Fields[i].DataType of
ftString :
begin
if AdoQuery.Fields[i].IsNull then
begin
FillMemory(Pointer(dwStart + dwPos), AdoQuery.Fields[i].DataSize -1, 0);
end
else
begin
StrPCopy(Pointer(dwStart + dwPos), AdoQuery.Fields[i].AsString);
nPos := 0;
for j := AdoQuery.Fields[i].DataSize -3 downto 0 do begin
chTmp := PChar(dwStart + dwPos + DWORD(j))^;
if (chTmp <> Chr(32)) and (chTmp <> Chr(0)) then begin
PChar(dwStart + dwPos + DWORD(j) +1)^ := Chr(0);
nPos := dwStart + dwPos + DWORD(j);
Break;
end;
end;
if (nPos = 0) then begin
FillMemory(Pointer(dwStart + dwPos), AdoQuery.Fields[i].DataSize, 0);
end;
end;
end;
ftWord : //(在Delphi中取出的tinyint类型为word,因此特殊处理)
begin
if AdoQuery.Fields[i].IsNull then
PByte(dwStart + dwPos)^ := 0
else
PByte(dwStart + dwPos)^ := Byte(AdoQuery.Fields[i].AsInteger);
end;
ftSmallint :
begin
if AdoQuery.Fields[i].IsNull then
PWORD(dwStart + dwPos)^ := 0
else
PWORD(dwStart + dwPos)^ := WORD(AdoQuery.Fields[i].AsInteger);
end;
ftInteger :
begin
if AdoQuery.Fields[i].IsNull then
PInt(dwStart + dwPos)^ := 0
else
PInt(dwStart + dwPos)^ := AdoQuery.Fields[i].AsInteger;
end;
ftFloat :
begin
if AdoQuery.Fields[i].IsNull then
PDouble(dwStart + dwPos)^ := 0.0
else
PDouble(dwStart + dwPos)^ := AdoQuery.Fields[i].AsFloat;
end;
ftBytes, ftVarBytes, ftFixedChar :
begin
if (AdoQuery.Fields[i].IsNull) then
FillMemory(Pointer(dwStart + dwPos), AdoQuery.Fields[i].DataSize, 0)
else
AdoQuery.Fields[i].GetData(Pointer(dwStart + dwPos));
end;
ftBlob :
begin
FillMemory(Pointer(dwStart + dwPos), AdoQuery.Fields[i].DataSize, 0);
end;
ftLargeint :
begin
AdoQuery.Fields[i].GetData(@nInt64);
PInt64(dwStart + dwPos)^ := nInt64;
end;
end;
if (AdoQuery.Fields[i].DataType = ftString) then
dwPos := dwPos + DWORD(AdoQuery.Fields[i].DataSize -1)
else if (AdoQuery.Fields[i].DataType = ftWord) then
dwPos := dwPos + DWORD(AdoQuery.Fields[i].DataSize) -1
else
dwPos := dwPos + DWORD(AdoQuery.Fields[i].DataSize);
end;
AdoQuery.Next;
nRow := nRow +1;
end;
end;
Result := AdoQuery.RecordCount;
except
Result := -1;
end;
if Assigned(AdoQuery) then begin
AdoQuery.Destroy;
end;
end;
function ExcuteCommand(szSql: PChar): Integer;
var
AdoQuery : TADOQuery;
begin
AdoQuery := nil;
try
AdoQuery := TADOQuery.Create(nil);
if not Assigned(AdoQuery) then begin
Result := -1;
Exit;
end;
AdoQuery.Connection := gConnection;
AdoQuery.Prepared := True;
AdoQuery.Close;
AdoQuery.SQL.Text := szSql;
AdoQuery.ExecSQL();
Result := 1;
except
Result := -1;
end;
if Assigned(AdoQuery) then begin
AdoQuery.Destroy;
end;
end;
function CloseDB(): Integer;
begin
if Assigned(gConnection) then begin
gConnection.Connected := False;
gConnection.Destroy();
end;
CoUninitialize();
Result := 1;
end;
function FreeAndNilMemory(p: PPointer): Integer;
begin
FreeMemory(p^);
p^ := nil;
Result := 1;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -