Sign in Join
Wiki copytemporary

mssqlconn

English · Español · Français · Polski

mssqlconn is an fcl-db unit containing connector code for TMSSQLConnection (MS SQL Server) and TSybaseConnection (Sybase ASE).

Lazarus uses mssqlconn to present an MS SQL connector and Sybase ASE connector.

DBLib

mssqlconn uses the dblib low-level unit which requires the FreeTDS library (.dll/.dylib/.so) to connect to the server.

Installation

The only thing you need is a FreeTDS dll/dylib/so for your platform.

Advanced use: by modifying FPC file dblib.pas SQLDB could use the "native" library ntwdblib.dll instead of the default FreeTDS library dblib.dll (in this case FPC needs to be recompiled).

Documentation

Official documentation: FPC documentation on mssqlconn

Limitations and tips

As the documentation specifies:

Change the drivername and/or position

Output parameters of stored procedures

Also, TMSSQLConnection and TSybaseConnection do not support handling of return status and output parameters of stored procedures. For more informations see: http://www.freetds.org/faq.html#ms.output.parameters As a workaround you can use something like:

pascal
with SQLQuery1 do begin
  SQL.Text := 'declare @param int; declare @ret int;' +
              'exec @ret=MyStoredProc 1, @param OUTPUT;' +
              'select @ret as return_status, @param as out_param';
  Open;
end;

Some more tips:

Error messages

Error 20009 : Unable to connect: Adaptive Server is unavailable or does not exist

Error 20019 : Attempt to initiate a new Adaptive Server operation with results pending

Error 20047 : DBPROCESS is dead or not enabled

EINOutError/Can not load DB-Lib client library "dblib.dll". Check your installation

Examples

Creating a database

The example below creates a database on an MS SQL Server. For this to work, the connection's parameter AutoCommit must be set to on. For normal subsequent connection you can switch it off again and use regular StartTransaction and Commit.

pascal
// Set up a form with a button called DBButton, 
// textbox called DatabaseNameEdit,
// '''MSSQLConnection''' called FConn
// ***make sure the Autocommit parameter is on for the create db part before connectiong:
//FConn.Params.Add('AutoCommit=true');
procedure TForm1.CreateDBButtonClick(Sender: TObject);
var
  CurrentDB: string;
begin
  if DatabaseNameEdit.Text = '' then
  begin
    showmessage('Empty database name. Please specify database name first.');
    exit;
  end;

  CurrentDB := FConn.DatabaseName;
  try
    // For FPC 2.6.1+
    FConn.ExecuteDirect('CREATE DATABASE ' + DatabaseNameEdit.Text);
    {
    // This works with FPC 2.7.1, not on 2.6.1
    FConn.CreateDB;
    }
  except
    on E: Exception do
    begin
      showmessage('Exception running CreateDB: ' + E.Message);
    end;
  end;
end;

From the Free Pascal wiki, page "mssqlconn", as it stood on 2021-08-12 after a change by Lacak, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.