Lazarus to MSSQL (2012 Express) Connection Issue lazarus / 16-Databases ---------------------------------------------------------------------- Madmensking · 2014-08-07 16:01 Hi, I'm having an issue connecting a new project to an existing MSSQL 2012 Express Edition Database running on a windows server 2008 R2 installation. I am attempting to use TSQLConnection and I keep getting a connection timeout error and cant see why, The server accepts connection (Using the same SQL Authenticated account from other programs and is configured to allow unlimited remote connections) The code i'm using is as follows: Code: [Select]procedure TForm1.Button1Click(Sender: TObject);   var      S: String;      conn: TSQLConnection;      query: TSQLQuery;      transaction: TSQLTransaction;   begin     Conn := TSQLConnection.Create(nil);     Query := TSQLQuery.create(nil);     Transaction := TSQLTransaction.create(nil);     try        try           Conn.HostName:= '';           Conn.DatabaseName:= '';           Conn.UserName := '';           Conn.Password := '';           Conn.Transaction := transaction;           Query.DataBase := conn;           Query.PacketRecords := -1;           Query.SQL.Text := 'Select * From DOL_NAMES.FName';           Query.Open;           S := '';           while not Query.EOF do           begin                S := S + Query.FieldByName('FName').AsString + #13#10;                Query.Next;           end;        finally               Query.Free;               Conn.Free;               Transaction.Free;        end;     except           on E: Exception do              ShowMessage (E.message);     end;     Memo1.Text := S;     Label1.Caption := 'Button Clicked'; end;        Any help, even if its just letting me know the code is sound so the problem is elsewhere would be greatly appreciated. Thanks in advance ---------------------------------------------------------------------- rvk · 2014-08-07 16:35 Isn't TSQLConnection an abstract class you shouldn't use directly? QuoteThis class will never be instantiated directly, for each database type a descendent class specific for this database type must be created. I think you need to use the TSQLConnector and set the ConnectorType to "MSSQLServer". So like this: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var   S: string;   conn: TSQLConnector;   query: TSQLQuery;   transaction: TSQLTransaction; begin   Conn := TSQLConnector.Create(nil);   Query := TSQLQuery.Create(nil);   Transaction := TSQLTransaction.Create(nil);   try     try       Conn.ConnectorType := 'MSSQLServer';       Conn.HostName := '';       Conn.DatabaseName := '';       Conn.UserName := '';       Conn.Password := '';       Conn.Transaction := transaction;       Query.DataBase := conn;       // .... ---------------------------------------------------------------------- BigChimp · 2014-08-07 17:08 Quote from: Madmensking on August 07, 2014, 06:01:35 pmThe server accepts connection (Using the same SQL Authenticated account from other programs and is Do you mean SSPI/Authenticated connections? Then leave username and password blank (IIRC). See the documentation on the connection. http://www.freepascal.org/docs-html/fcl/mssqlconn/index.html PS: Please replace the < and > in with [ and ] thanks. Edit: apart from that: what rvk said. You could also use the TMSSQLConnection directly. ---------------------------------------------------------------------- luca · 2014-08-07 18:37 In my program I prefer use TODBConnection to interface MSSQL. I tested other solutions (zeos in primis) but they need a dll to connect. I use ODBC to Mssql 2000, 2005, 2008 and AS/400 with no issues. I found really usefull to create external dsn files to be loaded when program started. This solution is very stable. You can create dsn via ODBC administrator and load it using this code: Code: [Select]db.Params.Add('FILEDSN=yourfilename.dsn'); db.Params.Add('AUTOCOMMIT=1'); where db is a TODBCConnection object. No need to create a static ODBC connection just deploy your  dsn file with your exe. Username and Password can be saved into dsn file. Regards ---------------------------------------------------------------------- BigChimp · 2014-08-08 08:39 Why use a DSN file when you can use a DSN-less connection? Just curious... ---------------------------------------------------------------------- taazz · 2014-08-08 09:11 Quote from: BigChimp on August 08, 2014, 10:39:28 amWhy use a DSN file when you can use a DSN-less connection? Just curious... easier to set/change with out the need to write a settings dialog? That would be my reason. ---------------------------------------------------------------------- BigChimp · 2014-08-08 09:31 Mmm ok, could be. If you are storing application settings anyway then you could add db connection stuff as well, but yes, you're right that is a possible reason. ---------------------------------------------------------------------- taazz · 2014-08-08 10:32 Quote from: BigChimp on August 08, 2014, 11:31:27 amMmm ok, could be. If you are storing application settings anyway then you could add db connection stuff as well, but yes, you're right that is a possible reason. not if you save them in the database to start with. ---------------------------------------------------------------------- BigChimp · 2014-08-08 10:35 Obviously. Nice playing "what if" games ---------------------------------------------------------------------- taazz · 2014-08-08 10:42 Really not a "what if" already worked in such an application where each user had his/her own settings saved on the database and access them from any terminal used. Thing of salesmen that come in once a week to type in their weekly report/ accounting info etc using any free computer in the building. No personal computer. ---------------------------------------------------------------------- BigChimp · 2014-08-08 10:45 Sure. I worked with applications that stored config settings in dbs and apps that stored them in files as well. It all depends on how OP set up his stuff... which he hasn't told us. ---------------------------------------------------------------------- luca · 2014-08-08 12:16 Just to clarify. My application is one exe file. It runs (actually) in four different sites. Every site uses a different DB servers with different versions of MSSQL. I've one ini file containing settings for the site. In this file I read the name of the dsn files to be used to reach db. Using a dsn file allow to set just one property in my ODBCConnetion object: the name of the file. Otherwyse I need to store a lot of different information dependending DBMS engine they want to use. In the future if they'll change DBMS no problem for different settings: they are all stored in dsn file. I found this solution really good. Regards Luca