Title: Let User Create a UDL-file from within your app.
Question: SQLBuilder is a tool we have developed - in testing now - for database 'professionals'. SQLBuilder connects via ADO to most databases and gives the user an easy to use tool for creating datasets and exporting those datasets to HTML reports or CSV files for use in MS EXCEL or so, and much more....
In SQLBuilder we have an option for the user to create and save an UDL (Microsoft Universal Data Link) file using the rather userfriendly ADO Connection String Dialog as found in ADOXpress, Delphi 5E and in some MS Windows versions.
Answer:
The procedure is simple enough.
It is clear that dataform, ado_conn1, mainform, new_udlbtn are from my app.
It is easy to see that what makes an UDL-file tick are some special characters here and there and null characters all over the place.
Important: include the unit AdoConEd in the uses statement, because this unit holds the ADO Connection String Dialog. The function editconnectionstring() invokes the ADO Connection String Dialog.
procedure Tmainform.newudl_btnClick(Sender: TObject);
var
Fs: Tfilestream;
cs1, cs2: string;
countvar: integer;
begin
try
dataform.ado_conn1.close;
if editconnectionstring(dataform.ado_conn1) then
begin
cs1 := '[oledb]' +
#13#10'; Everything after this line is an OLE DB initstring' +
#13#10'' + trim(dataform.ado_conn1.connectionstring) + #13#10'';
for countvar := 1 to length(cs1) do cs2 := cs2 + cs1[countvar] + #0;
cs2 := #255#254'' + cs2;
with mainform.savedialog1 do
begin
filename := '';
initialdir := mainunit.program_dir;
defaultext := 'UDL';
filter := 'UDL files (*.UDL)|*.UDL';
if execute then
begin
if fileexists(saveDialog1.FileName)
then deletefile(saveDialog1.FileName);
fs := tfilestream.create(saveDialog1.FileName, fmcreate);
fs.write(pchar(cs2)^, length(cs2));
fs.free;
end;
end;
end;
except
mainunit.bad('Sorry, something went wrong. Try again..');
fs.free;
end;
end;