Hoo*_*oot 2 delphi sql-server-ce
我试图使用本地.sdf文件作为临时存储的方法,如果主数据库无法访问.我有.sdf文件,但是当我尝试将其设置为文件时,似乎根本不知道.sdf是否存在.我目前的当前连接字符串是:
Driver={SQL Native Client};Data Source=C::\users\username\desktop\file\MyData.sdf;Persist Security Info=False
Run Code Online (Sandbox Code Playgroud)
并为它为我生成的提供者:
Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5
Run Code Online (Sandbox Code Playgroud)
当我尝试使用连接时,我收到"无法找到提供商.可能没有正确安装." .sdf绝对是在文件夹中.我还有一个问题,它想要一个用户名和/或密码,在创建数据库时我都不必指定.
问题:我的连接字符串有问题吗?使用ADO连接访问SQL Compact数据库是否合理?可能有一种更简单的方法来从临时存储中查询/检索数据(我更喜欢用SQL做它)?大多数文档似乎是从2003/2005开始的,这是无益的.
我使用"connectionstrings.com"来帮助制作字符串.任何建议都会有所帮助,谢谢
首先打开sdf文件,您必须使用与sdf文件版本兼容的提供程序.既然您在评论中提到版本3.5,则必须使用此提供程序Microsoft.SQLSERVER.CE.OLEDB.3.5
然后,您必须确保安装了哪个提供程序
尝试使用此代码列出系统中安装的OLEDB提供程序
{$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.
Run Code Online (Sandbox Code Playgroud)
现在,这是连接到Sql Server压缩文件的基本示例.
{$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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8429 次 |
| 最近记录: |