Difference between revisions of "mssqlconn"

From Lazarus wiki
Jump to navigationJump to search
(started again; needed page for examples etc)
 
(added example)
Line 1: Line 1:
 
==Overview==
 
==Overview==
mssqlconn is an fcl-db unit containing driver/connector code for Microsoft SQL Server and Sybase ASE.
+
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.
 
Lazarus uses mssqlconn to present an MS SQL connector and Sybase ASE connector.
  
 
==DBLib==
 
==DBLib==
 
mssqlconn uses the dblib low-level unit which requires the FreeTDS library (.dll/.dylib/.so) to connect to the server.
 
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
 +
* On Linux, download the freetds package which provides '''libsybdb.so''' (e.g. in Debian this is provided by package '''libsybd5''') (and development package) using your package manager.
 +
* On Windows, you can download a recent 32 or 64 bit version of the FreeTDS library '''dblib.dll''' here: [ftp://ftp.freepascal.org/fpc/contrib/windows/]
 +
 +
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==
 
==Documentation==
Official documentation:
+
Official documentation: [http://www.freepascal.org/docs-html/fcl/mssqlconn/index.html FPC documentation on mssqlconn]
 +
As the documentation specifies, you might need to tweak some parameters for BLOB support. Also, MS SQL multiple resultsets (MARS) are not supported.
  
 
==Examples==
 
==Examples==
 
===Creating a database===
 
===Creating a database===
The example below creates a database on an MS SQL Server:
+
The example below creates a database on an MS SQL Server. Note that for this to work, the connection's parameter AutoCommit must be set to on.
 +
<syntaxhighlight>
 +
// 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;
 +
</syntaxhighlight>
  
  

Revision as of 10:23, 20 April 2013

Overview

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

  • On Linux, download the freetds package which provides libsybdb.so (e.g. in Debian this is provided by package libsybd5) (and development package) using your package manager.
  • On Windows, you can download a recent 32 or 64 bit version of the FreeTDS library dblib.dll here: [1]

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 As the documentation specifies, you might need to tweak some parameters for BLOB support. Also, MS SQL multiple resultsets (MARS) are not supported.

Examples

Creating a database

The example below creates a database on an MS SQL Server. Note that for this to work, the connection's parameter AutoCommit must be set to on.

// 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;