mssqlconn: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: mssqlconn
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **mssqlconn** is an fcl-db unit containing connector code for [[TMSSQLConnection]] (MS SQL Server) and [[TSybaseConnection]] (Sybase ASE). | |
| 2 | + | ||
| 3 | + | Lazarus uses mssqlconn to present an MS SQL connector and Sybase ASE connector. | |
| 4 | + | ||
| 5 | + | ## DBLib | |
| 6 | + | ||
| 7 | + | mssqlconn uses the dblib low-level unit which requires the [FreeTDS](http://www.freetds.org) library (.dll/.dylib/.so) to connect to the server. | |
| 8 | + | ||
| 9 | + | ## Installation | |
| 10 | + | ||
| 11 | + | The only thing you need is a FreeTDS dll/dylib/so for your platform. | |
| 12 | + | ||
| 13 | + | - On Linux, download the freetds package which provides **libsybdb.so** (e.g. in Debian/Raspbian this is provided by package **libsybdb5**) and the related development package using your package manager. You might also need to add a symbolic link libsybdb.so to libsybdb.so.5 | |
| 14 | + | - Ubuntu 20.04 | |
| 15 | + | ||
| 16 | + | `sudo apt install libsybdb5` | |
| 17 | + | ||
| 18 | + | `cd /usr/lib/x86_64-linux-gnu` | |
| 19 | + | ||
| 20 | + | `sudo ln -s libsybdb.so.5.1.0 libsybdb.so` | |
| 21 | + | - 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/> or <http://downloads.freepascal.org/fpc/contrib/windows/> | |
| 22 | + | ||
| 23 | + | 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). | |
| 24 | + | ||
| 25 | + | ## Documentation | |
| 26 | + | ||
| 27 | + | Official documentation: [FPC documentation on mssqlconn](http://www.freepascal.org/docs-html/fcl/mssqlconn/index.html) | |
| 28 | + | ||
| 29 | + | ## Limitations and tips | |
| 30 | + | ||
| 31 | + | - Sybase port: the default port for Sybase ASE is 5000. As FreeTDS/mssqlconn uses 1433 by default, you may want to add :5000 to the hostname proprerty automatically or after feedback from the end user. | |
| 32 | + | ||
| 33 | + | As the documentation specifies: | |
| 34 | + | ||
| 35 | + | - you might need to tweak some parameters for BLOB support. | |
| 36 | + | - MS SQL multiple resultsets (MARS) are not supported. | |
| 37 | + | ||
| 38 | + | Change the drivername and/or position | |
| 39 | + | ||
| 40 | + | - Put dblib as first unit in your .lpr and then call dblib.initialisedblib('/position/to/libsybdb.so') | |
| 41 | + | ||
| 42 | + | ### Output parameters of stored procedures | |
| 43 | + | ||
| 44 | + | 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: | |
| 45 | + | ||
| 46 | + | ``` pascal | |
| 47 | + | with SQLQuery1 do begin | |
| 48 | + | SQL.Text := 'declare @param int; declare @ret int;' + | |
| 49 | + | 'exec @ret=MyStoredProc 1, @param OUTPUT;' + | |
| 50 | + | 'select @ret as return_status, @param as out_param'; | |
| 51 | + | Open; | |
| 52 | + | end; | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | Some more tips: | |
| 56 | + | ||
| 57 | + | - You might want to do an YourConnection.ExecuteDirect('SET ANSI_NULL_DFLT_ON ON'); if you want to automatically allow NULL values for columns when creating tables. See MS documentation: <http://msdn.microsoft.com/en-us/library/ms174979.aspx> | |
| 58 | + | - You might want to similarly set SET ANSI_PADDING to ON to enable better compatibiity with sqldb text comparison functions. See MS documentation: <http://msdn.microsoft.com/en-us/library/ms187403.aspx> | |
| 59 | + | ||
| 60 | + | ## Error messages | |
| 61 | + | ||
| 62 | + | ### Error 20009 : Unable to connect: Adaptive Server is unavailable or does not exist | |
| 63 | + | ||
| 64 | + | - Check if TCP/IP protocol is enabled on SQL Server side using "SQL Server Configuration Manager" - "SQL Server Network Configuration" | |
| 65 | + | - Check if TCP port 1433 is opened | |
| 66 | + | - If you connect to specific instance of SQL Server (this is also case of SQL Server Express), check if SQL Server Browser Service is running on server (service listens on UDP port 1434) | |
| 67 | + | ||
| 68 | + | ### Error 20019 : Attempt to initiate a new Adaptive Server operation with results pending | |
| 69 | + | ||
| 70 | + | - Set TSQLQuery.PacketRecords property to -1 | |
| 71 | + | - For more information see: <http://www.freetds.org/faq.html#pending> | |
| 72 | + | ||
| 73 | + | ### Error 20047 : DBPROCESS is dead or not enabled | |
| 74 | + | ||
| 75 | + | - SQL Server periodically verifies (by default every 30 seconds) if idle TCP/IP connection is still intact by sending a keep alive packet to its peer. If the remote system is still reachable and functioning, a acknowledge packet is sent back. If not local TCP will reset connection. | |
| 76 | + | - Adjust SQL Server Configuration Manager -\> SQL Server Network Configuration -\> Protocols -\> TCP/IP : "Keep Alive" | |
| 77 | + | ||
| 78 | + | ### EINOutError/Can not load DB-Lib client library "dblib.dll". Check your installation | |
| 79 | + | ||
| 80 | + | - Make sure you have all required dlls installed (e.g. **dblib.dll**, **libiconv2.dll**) | |
| 81 | + | - Make sure you have all required C/C++ runtime libraries installed that **dblib.dll** depends on | |
| 82 | + | - Make sure the bitness (32 or 64 bit) of the dblib library and your compiled program match | |
| 83 | + | ||
| 84 | + | ## Examples | |
| 85 | + | ||
| 86 | + | ### Creating a database | |
| 87 | + | ||
| 88 | + | 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**. | |
| 89 | + | ||
| 90 | + | ``` pascal | |
| 91 | + | // Set up a form with a button called DBButton, | |
| 92 | + | // textbox called DatabaseNameEdit, | |
| 93 | + | // '''MSSQLConnection''' called FConn | |
| 94 | + | // ***make sure the Autocommit parameter is on for the create db part before connectiong: | |
| 95 | + | //FConn.Params.Add('AutoCommit=true'); | |
| 96 | + | procedure TForm1.CreateDBButtonClick(Sender: TObject); | |
| 97 | + | var | |
| 98 | + | CurrentDB: string; | |
| 99 | + | begin | |
| 100 | + | if DatabaseNameEdit.Text = '' then | |
| 101 | + | begin | |
| 102 | + | showmessage('Empty database name. Please specify database name first.'); | |
| 103 | + | exit; | |
| 104 | + | end; | |
| 105 | + | ||
| 106 | + | CurrentDB := FConn.DatabaseName; | |
| 107 | + | try | |
| 108 | + | // For FPC 2.6.1+ | |
| 109 | + | FConn.ExecuteDirect('CREATE DATABASE ' + DatabaseNameEdit.Text); | |
| 110 | + | { | |
| 111 | + | // This works with FPC 2.7.1, not on 2.6.1 | |
| 112 | + | FConn.CreateDB; | |
| 113 | + | } | |
| 114 | + | except | |
| 115 | + | on E: Exception do | |
| 116 | + | begin | |
| 117 | + | showmessage('Exception running CreateDB: ' + E.Message); | |
| 118 | + | end; | |
| 119 | + | end; | |
| 120 | + | end; | |
| 121 | + | ``` | |
| 122 | + | ||
| 123 | + | --- | |
| 124 | + | *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](/wiki-contact).* |
