Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.93 KB | None | 0 0
  1. program ProjectMysqlADO;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   ActiveX,
  7.   DB,
  8.   ADODB,
  9.   SysUtils;
  10.  
  11. const
  12. //the connection string
  13. StrConnection='Driver={MySQL ODBC 3.51 Driver};Server=%s;Database=%s;User=%s; Password=%s;Option=3;';
  14.  
  15.  
  16. var
  17. AdoConnection : TADOConnection;
  18.  
  19. procedure SetupConnection(DataBase:String);//Open a connection
  20. begin
  21.   Writeln('Connecting to MySQL');
  22.   AdoConnection:=TADOConnection.Create(nil);
  23.   AdoConnection.LoginPrompt:=False;//dont ask for the login parameters
  24.   AdoConnection.ConnectionString:=Format(StrConnection,['your_server',DataBase,'your_user','your_password']);
  25.   AdoConnection.Connected:=True; //open the connection
  26.   Writeln('Connected');
  27. end;
  28.  
  29. procedure CloseConnection;//Close an open connection
  30. begin
  31.   Writeln('Closing connection to MySQL');
  32.   if AdoConnection.Connected then
  33.   AdoConnection.Close;
  34.   AdoConnection.Free;
  35.   Writeln('Connection closed');
  36. end;
  37.  
  38. procedure SearchPersonnel(Nom:String);//Search for an employee in the database
  39. var
  40.   AdoQuery : TADOQuery;
  41. begin
  42.    AdoQuery:=TADOQuery.Create(nil);
  43.    try
  44.     AdoQuery.Connection:=AdoConnection;
  45.     AdoQuery.SQL.Add('SELECT * FROM Temploye WHERE nom='+Nom);
  46.     AdoQuery.Open;
  47.     while not  AdoQuery.eof do
  48.     begin
  49.       Writeln(format('%s %s %s',[AdoQuery.FieldByname('id').AsString,AdoQuery.FieldByname('nom').AsString,AdoQuery.FieldByname('prenom').AsString]));
  50.       AdoQuery.Next;
  51.     end;
  52.    finally
  53.    AdoQuery.Free;
  54.    end;
  55. end;
  56.  
  57. begin
  58.   CoInitialize(nil); // call CoInitialize()
  59.   try
  60.        Writeln('Init');
  61.        try
  62.          SetupConnection('Dm'); //open the connection pointing to the Mydb database
  63.          SearchPersonnel('nom'); //search for an employee by his name
  64.          CloseConnection; //close the connection
  65.        except
  66.          on E : Exception do
  67.            Writeln(E.Classname, ': ', E.Message);
  68.        end;
  69.       Readln;
  70.   finally
  71.    CoUnInitialize; // free memory
  72.   end;
  73. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement