Connection a une base dev donnée sql server

anonymo Messages postés 2 Statut Membre -  
phil_232 Messages postés 286 Statut Membre -
Bonsoir tous le monde, je suis débutant et je veux un cours sur la connexion à une base de données sql server avec delphi 07
Configuration: Windows XP
Internet Explorer 6.0

1 réponse

  1. phil_232 Messages postés 286 Statut Membre 33
     
    //-------------------------------------------------------------------
    function ExecuteSQL(DB: string; sSQL: string): integer;
    begin
    ExecuteSQL := 0;
    try
    if DB = 'EOD' then begin
    cnEOD.Execute(sSQL, cmdText);
    end
    else if DB = 'UD' then begin
    cnUD.Execute(sSQL, cmdText);
    end
    else if DB = 'TM' then begin
    cnTM.Execute(sSQL, cmdText);
    end;
    except
    on E: Exception do begin
    ShowMessage('Error executing ExecuteSQL(' + DB + ', ' + sSQL + ') in DBUtil!!!' + g_CrLf + E.Message);
    end;
    end;
    end;
    //-------------------------------------------------------------------
    procedure ExecuteSQLWithRS(DB: string; var rs: _RecordSet; sSQL: string);
    begin
    try
    if DB = 'EOD' then begin
    rs := cnEOD.Execute(sSQL, cmdText);
    end
    else if DB = 'UD' then begin
    rs := cnUD.Execute(sSQL, cmdText);
    end
    else if DB = 'TM' then begin
    rs := cnTM.Execute(sSQL, cmdText);
    end;
    except
    on E: Exception do begin
    ShowMessage('Error getting the recordset from ' + DB + ' using ' + sSQL + ' in DBUtil!!!' + g_CrLf + sSQL + g_CrLf + E.Message);
    end;
    end;
    end;
    //-------------------------------------------------------------------
    procedure Init_DB(var Server, Database, UserID, Password: string);
    begin
    try

    //----------------------
    // Set audit variables
    //----------------------
    g_HostID := GetEnvironmentVariable('ComputerName');
    g_UserID := GetEnvironmentVariable('UserName');

    //------------------------------------
    // Create connection to EOD database
    //------------------------------------
    cnEOD := TADOConnection.Create(Application);
    if FileExists('ConnectionEOD.udl') then begin
    cnEOD.ConnectionString:='File Name=ConnectionEOD.udl';
    cnEOD.LoginPrompt := false;
    cnEOD.Open;
    end
    else begin
    MessageDlg('EOD connection file not found!!', mtError, [mbOk], 0);
    Exit;
    end;

    //------------------------------------
    // Create connection to UD database
    //------------------------------------
    cnUD := TADOConnection.Create(Application);
    if FileExists('ConnectionUD.udl') then begin
    cnUD.ConnectionString:='File Name=ConnectionUD.udl';
    cnUD.LoginPrompt := false;
    cnUD.Open;
    end
    else begin
    // MessageDlg('UD connection file not found!', mtError, [mbOk], 0);
    cnUD.ConnectionString:='File Name=ConnectionEOD.udl';
    cnUD.LoginPrompt := false;
    cnUD.Open;
    end;

    //------------------------------------
    // Create connection to TransModel database
    //------------------------------------
    cnTM := TADOConnection.Create(Application);
    if UserID = '' then begin
    cnTM.ConnectionString:='File Name=ConnectionTM.udl';
    cnTM.LoginPrompt := false;
    cnTM.Mode := cmRead;
    cnTM.Open;
    end
    else begin
    // cnTM.ConnectionString:='Provider=SQLOLEDB.1;Data Source=' + Server + ';Initial Catalog=' + Database + ';User ID=' + UserID + ';Password=' + Password + '';
    end;
    except
    on E: Exception do begin
    ShowMessage('Error opening the connection!!!');
    MessageDlg('Connection to database: ' + g_CrLf + E.Message, mtError, [mbOk], 0);
    Terminate_DB;
    end;
    end;
    end;

    fichier *.udl
    [oledb]
    ; Everything after this line is an OLE DB initstring
    Provider=SQLOLEDB.1;Password=xxxxxxxx;Persist Security Info=True;User ID=sa;Initial Catalog=MyDataBase;Data Source=MyServer
    1