简体   繁体   English

使用SQL Server 2008 R2与FireDAC查询

[英]Query with SQL Server 2008 R2 with FireDAC

I'm new to Firedac but not with Delphi 我是Firedac的新手,但不熟悉Delphi

I can make a connection with SQL Server, and when I connect, there is no problem. 我可以与SQL Server建立连接,连接时没有问题。 I get connection OK. 我连接正常。 It works with code, with the wizard and with the FireDAC explorer. 它可以与代码,向导和FireDAC Explorer一起使用。

The name of the database is : 该数据库的名称是:

C:\PROGRAM FILES (X86)\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\3410b539-431f-4dff-ad0d-a574d1b13498.MDF

I'm stuck with a simple query. 我陷入了一个简单的查询。

SELECT * 
FROM Account;

This query works from within the FireDac explorer but not from my program, I'm getting the following error: 此查询可在FireDac资源管理器中运行,但不能在我的程序中运行,出现以下错误:

[FireDAC][Phys][ODBC][Microsoft][SQL Server Native Client 11.0][SQL Server]Could not find server 'C:\\PROGRAM FILES (X86)\\MICROSOFT SQL SERVER\\MSSQL' in sys.servers. [FireDAC] [Phys] [ODBC] [Microsoft] [SQL Server Native Client 11.0] [SQL Server]在sys.servers中找不到服务器“ C:\\ PROGRAM FILES(X86)\\ MICROSOFT SQL SERVER \\ MSSQL”。 Verify that the correct server name was specified. 验证是否指定了正确的服务器名称。 If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.' 如有必要,执行存储过程sp_addlinkedserver将服务器添加到sys.servers。

I'm also also getting the same error, when I try to do it from the UI within Delphi. 当我尝试从Delphi中的UI进行操作时,我也遇到了相同的错误。

To try the query, I dropped onto the form a TFDQuery, the connection property was set automatically to the unique connection on the form. 为了尝试查询,我将TFDQuery放到表单上,将connection属性自动设置为表单上的唯一连接。 Then I set the query with the query editor, I get the error when I click on execute. 然后使用查询编辑器设置查询,单击执行时出现错误。

With code it like this : 用这样的代码:

FDConnection1.Connected := true;
FDQuery1.Close;
FDQuery1.SQL.Text := 'select * from Account;';
FDQuery1.Active := True;

When I debug the program, I get the error when setting the query active. 当我调试程序时,将查询设置为活动状态时会出现错误。

I see that the name of the DB is not complete in the error message. 我在错误消息中看到数据库的名称不完整。 But it's complete in the component settings 但这已在组件设置中完成

Please advise, 请指教,


I tried some extra test : If I run the query from the UI editor (double-clicking on the FDConnection, Tab SQL Script), type the query, it works :-). 我尝试了一些额外的测试:如果我从UI编辑器运行查询(双击FDConnection,Tab SQL脚本),则键入查询,它就可以工作了:-)。

But when I try to do it on the FDQuery component, with the connection assigned to previous FDConnection, I get the same error. 但是,当我尝试将FDQuery组件上的连接分配给以前的FDConnection时,会遇到相同的错误。 It' like some parameters are missing. 好像缺少一些参数。

You are using the location of the SQL data file, instead of the name of the SQL Database. 您使用的是SQL数据文件的位置,而不是SQL数据库的名称。

If you do not know the name you can use the wizard to try and find it. 如果您不知道名称,则可以使用向导尝试查找它。

(Sorry could not add comments yet. This is also mentioned by Jens in the comments) (对不起,还无法添加评论。Jens在评论中也提到了这一点)

Update: if that is really the name, try enclosing it in [ ]. 更新:如果确实是该名称,请尝试将其包含在[]中。

I got this response from an Embarcadero engineer: 我收到了Embarcadero工程师的以下回复:

The problem is not in FireDAC, but in SQL Server ODBC driver SQLPrimaryKeys function. 问题不在FireDAC中,而在SQL Server ODBC驱动程序SQLPrimaryKeys函数中。 It fails to work with a catalog name containing a dot. 它无法使用包含点的目录名称。 FireDAC uses this function to get primary key fields for a result set, when fiMeta is included into FetchOptions.Items. 当fiMeta包含在FetchOptions.Items中时,FireDAC使用此功能获取结果集的主键字段。 So, as a workaround / solution, please exclude fiMeta from FetchOptions.Items. 因此,作为解决方法/解决方案,请从FetchOptions.Items中排除fiMeta。

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

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