Pages

Thursday 22 November 2012

How to make database connection in delphi xe2 using TDatabase?

How to make database connection in delphi xe2 using TDatabase?

Following article shows how to make database connection in delphi xe2 using TDatabase. We have made a function called MakeConnection in which all the code for making database connection in delphi xe2 is written.

Take TDatabase delphi component on you delphi form (dfm file). Now set following setting to TDatabase delphi component.

object database1: TDatabase
    DatabaseName = 'YourDatabaseName'
    DriverName = 'YourDriver'
    LoginPrompt = False
    SessionName = 'Default'
 end

Now, come to your pas file and create a function say MakeConnection like below:

function TMyForm.MakeConnection;
begin
  try 
    MyForm.database1.Close;
    MyForm.database1.Params.Values['SERVER NAME'] := Servername;
    MyForm.database1.Params.Values['USER NAME']   := Username;
    MyForm.database1.Params.Values['PASSWORD']    := Password;
    MyForm.database1.Open;
   
    if database1.Connected then
       ShowMessage('Database connected')
    else
       ShowMessage('Database could not be connected')
  except
    on E : Exception do
    begin
      ShowMessage('Database Connection Error: ' + E.Message);
      exit;
    end;
  end;
end;

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.