Delphi 2010 and SQL 2005/2008

  • Want to connect to SQL 2005 or SQL 2008 from Delphi.

    New to both.

    Tried TSQLConnection but cannot get it to connect. Seems like a driver problem.

    Are there drivers?

  • Not sure of your environment. From my Delphi 7 (Win32) days I've always used ADO/OLEDB. Something in the following might help...

    uses ADODb;

    // If using SQL authentication, you'll get a login box if you have

    // uses DbLogDlg;

    // and

    // FConnection.LoginPrompt:=true;

    procedure TFormName.Login;

    const

    MSG_BAD_LOGIN = 'Login failed: incorrect authentication data,' + #13#10 +

    'or username and password do not match' + #13#10 + #13#10 +

    'Please retry';

    var

    FConnection: TADOConnection;

    FDbName: string;

    FDbServer: string;

    begin

    // FConnection:=TADOConnection.Create(self); // you'll probably instantiate it in the form

    FDbName:='Database_name'; // whatever...

    FDbServer:='Server_name'; // ...

    FConnection.Name:='Name_for_Database';

    FConnection.LoginPrompt:=true; // for SQL login dialogue

    FConnection.KeepConnection:=true;

    if not FConnection.Connected then

    begin

    Screen.Cursor:=crHourGlass;

    FConnection.ConnectionString:= 'Provider=SQLOLEDB.1' +

    ';Persist Security Info=False' +

    ';Initial Catalog=' + FDbName +

    ';Data Source=' + FDbServer ;

    // If using Windows authentication, connection string needs ';Integrated Security=SSPI'

    try

    FConnection.Open;

    except on e: exception do

    begin

    Screen.Cursor:=crDefault;

    if e is EOleException then

    begin

    case (e as EOleException).ErrorCode of

    E_FAIL: MessageDlg(MSG_BAD_LOGIN, mtError, [mbOK], 0);

    E_USER_CANC: ; // no message if Esc or Cancel pressed

    else

    MessageDlg(e.ClassName + ': ' + e.Message, mtError, [mbOK], 0);

    end;

    end

    else

    MessageDlg(e.ClassName + ': ' + e.Message, mtError, [mbOK], 0);

    end;

    end;

    end;

    Screen.Cursor:=crDefault;

    end;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply