Selamat Datang !

Selamat Datang di Yus Waroeng Software ! Saya ucapkan terima kasih anda sudah masuk ke blog kami, dimana anda dapat mencari informasi Software Aplikasi yang anda butuhkan dan berbagi ilmu pemograman.
Cari Artikel

Friday, March 28, 2008

Koneksi Database Dengan ADO

Halaman : 3
MAKE AN ADODB CONNECTION USING OLE-AUTOMATION

// ******************************************************************
// MAKE AN ADODB CONNECTION USING OLE-AUTOMATION
// Category : ADO
// ******************************************************************

{…}
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;

MS-SQL : CONNECTION IS IN USE BY ANOTHER STATEMENT

// ******************************************************************
// MS-SQL : CONNECTION IS IN USE BY ANOTHER STATEMENT
// Category : ADO
// ******************************************************************

{
When porting a larger database application (130k LOC) that worked fine with
Oracle and InterBase to MS-SQL (6.5), I frequently got the error message
‘connection is in use by another statement’.

At first, creating a new TDatabase for each TTable/ TQuery seemed to be
necessary.

Then I found what was ‘wrong’ (not really wrong.. :-)

To speed up some of my queries, I had set the property Unidirectional to true.
Delphi creates for such queries only one cursor (versus two for bidirectional
queries or TTables). After removing the assignments of Unidirectional := true
the error message disappeared and everything worked fine.

The following code resulted in the exception ‘connection is in use by another
statement’:
}

// dataBaseNameS : string is the name of the alias (MS-SQL 6.5)
begin
Query1 := TQuery.Create (Application);
With Query1 do
begin
DatabaseName := dataBaseNameS;
SQL.Text := ‘SELECT * FROM ABLESTOP’;
// the exception disappears if the following is removed
Unidirectional := True;
Open;
end;
ShowMessage (’ok’)

Table1 := TTable.Create (Self);
With Table1 do
begin
DatabaseName := dataBaseNameS;
TableName := ‘COMPONENT_PLAN’;
UpdateMode := upWhereKeyOnly;
Open
end;

Table1.Insert;
Table1.FieldByName (’PARTNO’).AsString := IntToStr (GetTickCount);
Table1.FieldByName (’ID’).AsString := ‘WWxx’;
Table1.FieldByName (’VERSION’).AsInteger := 1;
// the exception will occurr in the next statement:
// “Connection is in use by another statement”
Table1.Post;
end;

DETECT, WHICH VERSION OF ADO IS INSTALLED

// ******************************************************************
// DETECT, WHICH VERSION OF ADO IS INSTALLED
// Category : ADO
// ******************************************************************

{
With different versions of MDAC available it is sometimes useful to know that your a
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
end;
<>

<- Halaman sebelumnya

Halaman pertama ->

0 comments:

Post a Comment

Leave Your Comment Here!

Komentar Terbaru