简体   繁体   English

如何在Delphi中创建SQL Server CE数据库?

[英]How to create SQL Server CE database in Delphi?

I wrote an application in C#, which stores its data in a SQL Server CE database (v3.5). 我在C#中编写了一个应用程序,它将数据存储在SQL Server CE数据库(v3.5)中。 I would like to use these data from a Delphi application. 我想从Delphi应用程序中使用这些数据。 I found information about how to access an SDF file from Delphi code , so this part is clean and simple. 我找到了有关如何从Delphi代码访问SDF文件的信息 ,因此这部分简洁明了。

Unfortunately, there are cases when the Delphi application should be able to create the SDF file first. 不幸的是,有些情况下Delphi应用程序应该能够首先创建SDF文件。 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. 我想我需要在C#(DataContext子类)中编写ORM DLL并从两个应用程序(从Delphi到COM Interop)使用它,但我不确定这是个好主意。 Any advice would be greatly appreciated. 任何建议将不胜感激。

(I have Delphi 6 and XE2.) (我有Delphi 6和XE2。)

You can use the Create method of the Catalog Object (ADOX) 您可以使用目录对象Create方法(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.

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.) 如果从菜单中选择“项目”>“导入类型库”,则会在(长!)列表中找到名为“Microsoft ADO Ext.2.8 for DLL and Security(2.8版)”的条目(可能是旧版本或更新版本,但是它也应该工作。)

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. 创建单元,并使用CoCatalog.Create创建Catalog对象,并使用其Create方法创建由连接字符串指定的数据库。 Try with the connection string you've been using to connect to the SDF file. 尝试使用连接字符串连接到SDF文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM