Page 1 of 1

Connect programmatically using Delphi problem

Posted: Mon 12 Jan 2009 12:15
by John Whitehead
I am currently evaluating the SQL Server DBExpress drivers on Delphi before ordering.
I have encountered a problem when trying to programmatically connect to SQL Server Express 2005 during runtime. These are the steps I take:

1. Put a TSQLConnection component (SQLConnection1) on the form.

2. In design-time set the property DriverName to 'DevartSQLServer'. This also automatically sets the following properties:

GetDriverFunc = 'getSQLDriverSQLServer'
LibraryName = 'dbexpsda40.dll'
VendorLib = 'sqloledb.dll'

3. Put a button on the form, on the OnClick event write the following code:

SQLConnection1.Params.Add('HostName=\SQLEXPRESS');
SQLConnection1.Params.Add('DataBase=AdventureWorks');
SQLConnection1.Connected := True;

4. Compile and run the program. Click on the button and the error message 'Connection name missing.' is displayed.

If the parameters in Step 3 are done during design-time, I can connect without a problem. The steps are:

Steps 1 and 2 as above.

3. In design-time click on the property Params and set the following attributes:

Key Value
----------------------------------------
HostName \SQLEXPRESS
DataBase AdventureWorks

4. Put a button on the form, on the OnClick event write the following code:

SQLConnection1.Connected := True;

5. Compile and run the program. Click on the button and a connection will be made.

My development environment is:

Windows Vista Ultimate
Delphi 2007 Professional
Installed trial SQL Server DBExpress drivers
SQL Server Express 2005 installed on a Windows XP machine

Can the connection be made programmatically (if so, how?) or is this just a restriction because of the trial driver?

Posted: Mon 12 Jan 2009 16:06
by Dimon
The point is that the HostName and DataBase parameters already exist in the parameters list and you add new parameters with the same names.
To solve the problem you should set parameters values using the Values property, like this:

Code: Select all

SQLConnection1.Params.Values['HostName'] := '\SQLEXPRESS';
SQLConnection1.Params.Values['DataBase'] := 'AdventureWorks';

Posted: Mon 12 Jan 2009 16:45
by John Whitehead
Thanks for the quick solution. Silly me really :D