Connection a une base dev donnée sql server

Fermé
anonymo Messages postés 2 Date d'inscription jeudi 24 janvier 2008 Statut Membre Dernière intervention 24 janvier 2008 - 24 janv. 2008 à 21:06
phil_232 Messages postés 286 Date d'inscription jeudi 6 décembre 2007 Statut Membre Dernière intervention 12 juin 2008 - 27 janv. 2008 à 11:17
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
A voir également:

1 réponse

phil_232 Messages postés 286 Date d'inscription jeudi 6 décembre 2007 Statut Membre Dernière intervention 12 juin 2008 33
27 janv. 2008 à 11:17
//-------------------------------------------------------------------
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