Android smartphone

Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

2010-01-25

detect, which version of ADO is installed ?

Author: Simon Carter

With different versions of MDAC available it is sometimes

useful to know that your application won't fail because a user
hasn't got the latest version installed.
The following function returns the ADO version installed,
you need to place ComObj in the uses clause to use this function. }

function GetADOVersion: Double;
var
ADO: Variant;
begin
try
ADO := CreateOLEObject('adodb.connection');
Result := StrToFloat(ADO.Version);
ADO := Null;
except
Result := 0.0;
end;
end;

// To use this function try something like:

procedure TForm1.Button1Click(Sender: TObject);
const
ADOVersionNeeded = 2.5;
begin
if
GetADOVersion <>then
ShowMessage('Need to install MDAC version 2.7')
else
ShowMessage(Format('ADO Version %n, is OK', [GetADOVersion]));
end;

make an ADODB Connection using OLE-Automation ?

Author: Daniel Henrique Monteiro de Carvalho (Brasil)

{...}

uses
ComObj;
{...}

function OpenConnection(ConnectionString: AnsiString): integer;
var
ADODBConnection: OleVariant;
begin
ADODBConnection := CreateOleObject('ADODB.Connection');
ADODBConnection.CursorLocation := 3; // User client
ADODBConnection.ConnectionString := ConnectionString;
Result := 0;
try
ADODBConnection.Open;
except
Result := -1;
end;
end;

function DataBaseConnection_Test(bMessage: boolean): AnsiString;
var
asTimeout,
asUserName,
asPassword,
asDataSource,
ConnectionString: AnsiString;
iReturn: Integer;
OldCursor: TCursor;
begin
OldCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
asTimeout := '150';
asUserName := 'NT_Server';
asPassword := 'SA';
asDataSource := 'SQL Server - My DataBase';

ConnectionString := 'Data Source = ' + asDataSource +
'User ID = ' + asUserName +
'Password = ' + asPassword +
'Mode = Read|Write;Connect Timeout = ' + asTimeout;
try
iReturn := OpenConnection(ConnectionString);

if (bMessage) then
begin
if
(iReturn = 0) then
Application.MessageBox('Connection OK!', 'Information', MB_OK)
else if (iReturn = -1) then
Application.MessageBox('Connection Error!', 'Error', MB_ICONERROR

+ MB_OK);
end;
if (iReturn = 0) then

Result := ConnectionString
else if (iReturn = -1) then
Result := '';
finally
Screen.Cursor := OldCursor;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
DataBaseConnection_Test(true);
end;

check, if the Borland Database Engine (BDE) is installed ?

Author: tom

Uses
Bde;

function BDEInstalled : boolean;
begin
result := (dbiInit(nil) = 0)
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if
BDEInstalled then
ShowMessage('BDE is installed.')
else
ShowMessage('BDE is not installed.')
end;


Get the table version ?

Author: SwissDelphiCenter.ch

Homepage: http://www.swissdelphicenter.ch

function GetTableVersion(table: TTable): LongInt;
var
hCursor : hDBICur;
DT : TBLFullDesc;
begin
Check(DbiOpenTableList(table.DBHandle, True, False,
PChar(Table.TableName), hCursor));
Check(DbiGetNextRecord(hCursor, dbiNOLOCK, @DT, nil));
Result := DT.tblExt.iRestrVersion;
Check(DbiCloseCursor(hCursor));
end;

2009-10-12

...Select a random datarecord ?

Author: SwissDelphiCenter.ch

procedure TForm1.FormCreate(Sender:TObject);
begin
randomize;
end;

procedure TForm1.Button1Click(Sender:TObject);
begin
table1.First;
table1.MoveBy(Random(table1.recordcount));
end;

..Add a password to paradox table.

Author: SwissDelphiCenter.ch

unit Unit2;

interface
uses
bde,SysUtils,dbtables,windows;

Function TablePasswort(var table: ttable; password:string) : boolean;


implementation


function
StrToOem(const AnsiStr: string): string;
begin
SetLength(Result, Length(AnsiStr));
if Length(Result) > 0 then
CharToOem(PChar(AnsiStr), PChar(Result));
end;


Function TablePasswort(var table: TTable; password:string) : boolean;
var pTblDesc : pCRTblDesc;
hDb : hDBIDb;

begin
Result := false;
with Table do

begin
if
Active and (not Exclusive) then Close;
if (not Exclusive) then Exclusive := True;
if (not Active) then Open;
hDB := DBHandle;
Close;
end;
GetMem(pTblDesc,SizeOf(CRTblDesc));
FillChar(pTblDesc^,SizeOf(CRTblDesc),0 );
with pTblDesc^ do

begin
StrPCopy(szTblName, StrToOem(table.tablename));
szTblType:= szParadox;
StrPCopy(szPassword, StrToOem(Password));
bPack := True;
bProtected := True;
end;
if DbiDoRestructure(hDb, 1, pTblDesc, nil, nil, nil, FALSE )<> DBIERR_NONE

then exit;
if pTblDesc <> nil then FreeMem(pTblDesc, SizeOf(CRTblDesc));
result := true;
end;

..Show delete record.

Author: SwissDelphiCenter.ch

procedure deletedrecords(Table: TTable; SioNo: Boolean);
begin
Table.DisableControls;
try
Check(DbiSetProp(hDBIObj(Table.Handle), curSOFTDELETEON,
LongInt(SioNo)));
finally
Table.EnableControls;
end;
Table.Refresh;
end;

...open a password-saved paradox database without knowing the password ?

Author: Babak Sateli

If you forget your paradox table password ,then try this password , it works well

For PARADOX 7.0 use this password :"jIGGAe" or "cupcdvum"
For PARADOX 5.0 use this password : same thing
For PARADOX 4.x (DOS) use this password : "nx66ppx"

...undelete a dBASE record ?

Author: Doug Baird

procedure RecordUndelete(aTable: TTable);
begin
aTable.UpdateCursorPos;
try
Check(DbiUndeleteRecord(aTable.Handle));
except
ShowMessage('No undelete performed.');
end;
end;


2009-10-11

save a Formula One spreadsheet to a blob field

Author: Brett McLean

Uses vcf1, dbtables;


procedure SaveSpreadsheet(F1Book: TF1Book);
var
BlobStream : TBlobStream;
MyBlob : HGlobal;
pblob : Pointer;
begin
with
Datamodule1.Query1 do
begin
Set8087CW($133f);
try
Application.ProcessMessages;
F1Book.SaveWindowInfo;
MyBlob:=GlobalAlloc(GMEM_MOVEABLE, 2000);
try
F1Book.WriteToBlob(MyBlob, 0);
pBlob:=globalLock(MyBlob);
try
Blobstream:=TBlobStream.Create(TBlobField(FieldByName

('QUOTE_BLOB')),bmWrite);
try
Blobstream.Write(pBlob^,GlobalSize(myBlob));
finally
Blobstream.Free;
end;
finally
globalUnlock(MyBlob);
end;
F1book.IF1Book_Modified:=False;
finally
globalFree(myblob);
end;
finally
Set8087CW(Default8087CW);
Application.ProcessMessages;
end;
end;
end;

//Depending on your Delphi Version (<D4), you will need:
//D4 und frühere Versionen brauchen noch:

var
Default8087CW: Word = $1332;

procedure Set8087CW(NewCW: Word);
asm
MOV Default8087CW,AX
FLDCW Default8087CW
end;



Hotmail: Trusted email with powerful SPAM protection. Sign up now.

Remove a dbase index flag


Author: Alex Carrillo

function UnCheckIndex( FileDbf: String ): Boolean;
var
Dbf: File;
Car: Char;
begin
Result:= T;
AssignFile( Dbf, FileDbf );
Car := #0;
{$I-}
Reset(Dbf, 1);
If Not ErrorIO( FileDbf, IoResult ) then

Begin
Seek(Dbf, 28);
{Flag's position}
If Not ErrorIO( FileDbf, IoResult ) then
BlockWrite( Dbf, Car, 1, Num_R )
Else
Result:= F;
CloseFile(Dbf);
If ErrorIO( FileDbf, IoResult ) then
Result:= F;
End
Else
Result:= F;
{$I+}
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
If
UnCheckIndex('MyBase.dbf') Then
ShowMessage('Flag removed');
end;




Hotmail: Trusted email with Microsoft's powerful SPAM protection. Sign up now.

...pack a Table ?

...pack a Table ?

Author: Bernhard Angerer

Homepage: http://www.bluestep.com/

function dgPackParadoxTable(Tbl: TTable; Db: TDatabase):DBIResult;

{ Packs a Paradox table by calling the BDE DbiDoRestructure
function. The TTable passed as the first parameter must
be closed. The TDatabase passed as the second parameter
must be connected. }

var
TblDesc: CRTblDesc;
begin
Result := DBIERR_NA;
FillChar(TblDesc, SizeOf(CRTblDesc), 0);
StrPCopy(TblDesc.szTblName, Tbl.TableName);
TblDesc.bPack := True;
Result := DbiDoRestructure(Db.Handle, 1, @TblDesc,
nil, nil, nil, False);
end;



function dgPackDbaseTable(Tbl: TTable): DBIResult;

{ Pack a dBASE table by calling DbiPackTable. The table
passed as a parameter will be opened if it isn't open. }

begin
Result := DBIERR_NA;
if Tbl.Active = False then
Tbl.Open;
Result := DbiPackTable(Tbl.DBHandle, Tbl.Handle,
nil, nil, True);
end;




Hotmail: Trusted email with Microsoft's powerful SPAM protection. Sign up now.

2009-10-08

Select all Fields in a TDBGrid

function GridSelectAll(Grid:
TDBGrid): Longint;
begin
Result := 0;
Grid.SelectedRows.Clear; with Grid.Datasource.DataSet do
begin
First;
DisableControls;
try
while
not EOF do
begin
Grid.SelectedRows.CurrentRowSelected := True;
inc(Result);
Next;
end;
finally
EnableControls;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridSelectAll(DBGrid1);

end;

Extract the ODBC System Data Sources

Uses
Registry;

procedure TForm1.Button1Click(Sender: TObject);
var
n : Integer;
List : TStringList;
Reg : TRegistry;
begin
Reg
:= TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
Reg.LazyWrite := false; Reg.OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',
false);
List := TStringList.Create;
Reg.GetValueNames(List);
ListBox1.Clear;
for
n := 0 to List.Count-1 do
ListBox1.Items.Add(List.Strings[n]);
Reg.CloseKey;
finally
Reg.Free;
end;
end


Update by button


Use TDBEdit toenable users to edit a database field. TDBEdit uses the Text
property to represent the contents of the field.
But:

procedure TForm1.Button1Click(Sender: TObject);
begin
Table1.Edit; // Edit modeenabled
DBEdit1.Text:='ThanhMai'; //Assign Text
Table1.Post; // Write todatabase
end;

Table1 can't update!
My Solution:

procedure TForm1.Button1Click(Sender: TObject);
begin
Table1.Edit; // Edit modeenabled
DBEdit1.SetFocus; // Setsfocus to DBEdit1 !!!
DBEdit1.Text:='ThanhMai'; //Assign Text
Table1.Post; // Write todatabase
end;

Setting Bookmarks in a database

Setting Bookmarks in a database




Setting Bookmarks in a database:

Uses DB;

var Bookmark : TBookmark;
procedure TForm1.FormCreate(Sender: TObject);
begin
Bookmark := nil;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if BookMark <> nil
then Table1.GotoBookMark(BookMark);
Table1.FreeBookMark(BookMark);
Bookmark := nil;
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
if Bookmark = nil then
BookMark :=
Table1.GetBookMark;
end;

2009-10-05

How to create Paradox-Tables ?

How to create Paradox-Tables ?

2001-04-27 19:32:39


Use this bit of code for creating paradox tables.

\\===============================


Uses DB, DBTables;


with TTable.create(self) do
begin
DatabaseName := 'DBDEMONS';
TableName := 'Temp.db';
TableType := ttParadox;
with FieldDefs do
Begin
FieldDefs.Add('Age', ftInteger, 0, True);
FieldDefs.Add('Name', ftString, 25, False);
FieldDefs.Add('Weight', ftFloat, 0, False);
End;
IndexDefs.Add('','Age', [ixPrimary, ixUnique]);
CreateTable;
End;

How to create a view in Paradox ?

How to create a view in Paradox ?

2001-04-27 19:31:52


create a view on Paradox or any other standard table, do the following. Write the SQL statement whose result you want to query in a .SQL file and place the file into the data folder. e.g. create a file called view.sql with the following content



SELECT ORD.OrderNo , CUST.Company , EMP.FirstName , EMP.LastName
FROM CUSTOMER CUST , ORDERS ORD , EMPLOYEE EMP
WHERE CUST.CustNo = ORD.CustNo AND EMP.EmpNo = ORD.EmpNo



Here I have taken the tables from DBDEMOS alias. Save the file in the directory where DBDEMOS points to. Then you can write a query like this in your application


select *
from "view.sql"
order by FirstName, LastName T


his way one can create an equivalent of a view in Paradox. The advantages of this are enormous.