2
votes

I wrote an application in C#, which stores its data in a SQL Server CE database (v3.5). I would like to use these data from a Delphi application. I found information about how to access an SDF file from Delphi code, so this part is clean and simple.

Unfortunately, there are cases when the Delphi application should be able to create the SDF file first. I guess I need to write an ORM DLL in C# (the DataContext subclass) and use it from both applications (from Delphi through COM Interop), but I'm not sure this is a good idea. Any advice would be greatly appreciated.

(I have Delphi 6 and XE2.)

2
Check out this extensive list of third-party SQL Server CE tools and libraries - bet you'll find something on there that works as a standalone tool !marc_s
I have a command line utility, that you could include with your app - sqlcecmd.codeplex.comErikEJ
@LexLi, what does evaluating BlackFishSQL have to do with the question asked? "I need help fixing my Ferrari." "Did you test drive a Ford?"Ken White
@ErikEJ Thank you for the link!kol

2 Answers

6
votes

You can use the Create method of the Catalog Object (ADOX)

Try this sample app

{$APPTYPE CONSOLE}

{$R *.res}

uses
  ActiveX,
  ComObj,
  SysUtils;

procedure CreateSQLCeDatabase(const DataBase : string);
Var
  Catalog : OleVariant;
begin
  Catalog := CreateOleObject('ADOX.Catalog');
  Catalog.Create(Format('Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=%s',[DataBase]));
end;

begin
 try
    CoInitialize(nil);
    try
      CreateSQLCeDatabase('C:\Data\Bar.sdf');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
2
votes

If you select Project > Import Type Library from the menu, you'll find an entry in the (long!) list named "Microsoft ADO Ext. 2.8 for DLL and Security (Version 2.8)" (perhaps an older or newer version, but it should work just as well.)

Create the unit, and use CoCatalog.Create to create a Catalog object, and use its Create method to create a database as specified by a connection string. Try with the connection string you've been using to connect to the SDF file.