Connecting to a SQL Compact file (.sdf) using an ADO connection in Delphi

孤人 提交于 2019-12-01 10:35:22

First to open the sdf file you must use a provider compatible with the version of the sdf file. since you mention in your comments the version 3.5 you must use this provider Microsoft.SQLSERVER.CE.OLEDB.3.5

Then you must ensure which the provider is installed

Try this code to list the OLEDB providers installed in your system

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  Registry,
  Classes,
  SysUtils;

procedure ListOLEDBProviders;
var
  LRegistry: TRegistry;
  LIndex: Integer;
  SubKeys,Values: TStrings;
  CurKey, CurSubKey: string;
begin
  LRegistry := TRegistry.Create;
  try
    LRegistry.RootKey := HKEY_CLASSES_ROOT;
    if LRegistry.OpenKeyReadOnly('CLSID') then
    begin
      SubKeys := TStringList.Create;
      try
        LRegistry.GetKeyNames(SubKeys);
        LRegistry.CloseKey;
        for LIndex := 0 to SubKeys.Count - 1 do
        begin
          CurKey := 'CLSID\' + SubKeys[LIndex];
          if LRegistry.KeyExists(CurKey)  then
          begin
            if LRegistry.OpenKeyReadOnly(CurKey) then
            begin
              Values:=TStringList.Create;
              try
                LRegistry.GetValueNames(Values);
                LRegistry.CloseKey;
                for CurSubKey in Values do
                 if SameText(CurSubKey, 'OLEDB_SERVICES') then
                   if LRegistry.OpenKeyReadOnly(CurKey+'\ProgID') then
                   begin
                     Writeln(LRegistry.ReadString(''));
                     LRegistry.CloseKey;
                     if LRegistry.OpenKeyReadOnly(CurKey+'\OLE DB Provider') then
                     begin
                      Writeln('    '+LRegistry.ReadString(''));
                      LRegistry.CloseKey;
                     end;
                   end;
              finally
                Values.Free;
              end;
            end;
          end;
        end;
      finally
        SubKeys.Free;
      end;
      LRegistry.CloseKey;
    end;
  finally
    LRegistry.Free;
  end;
end;


begin
 try
    ListOLEDBProviders;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

Now this is a basic sample to connect to a Sql Server compact file.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  ActiveX,
  ComObj,
  AdoDb,
  SysUtils;

procedure Test;
Var
  AdoQuery : TADOQuery;
begin
  AdoQuery:=TADOQuery.Create(nil);
  try
    AdoQuery.ConnectionString:='Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\Datos\Northwind.sdf';
    AdoQuery.SQL.Text:='Select * from Customers';
    AdoQuery.Open;
    While not AdoQuery.eof do
    begin
      Writeln(Format('%s %s',[AdoQuery.FieldByName('Customer ID').AsString,AdoQuery.FieldByName('Company Name').AsString]));
      AdoQuery.Next;
    end;
  finally
    AdoQuery.Free;
  end;
end;

begin
 try
    CoInitialize(nil);
    try
      Test;
    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.

also check this wich helps with protected databases

"Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0; data source=\NorthWind.sdf; SSCE:Database Password="

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!