FireDAC 连接SQL Server一些要注意的地方(转)

时间:2022-07-28 05:11:44

TFDConnection:

FetchOptions.Mode 设置为fmAll, 返回全部结果, 否则默认只返回前50条, 效果与open以后再执行FetchAll一样

Specifies how the result set records must be fetched into FireDAC internal data storage.

Name Description
fmManual

The records are not fetched automatically by FireDAC. A programmer must write code to fetch records at appropriate times, using the following methods:

fmOnDemand The records are fetched automatically by FireDAC when they are required by the navigation or other dataset methods. At each request FireDAC fetches one or more record sets, containing RowsetSize records.

This mode allows to open large result sets without a significant delay, similarly to the fmAllmode. However, the total time of fetching all records is higher than in fmAll mode. For example, the server-side static cursor is used on Microsoft SQL Server.

fmAll All result set records are fetched automatically by FireDAC at the dataset Open call or at the first IADPhysCommand.Fetch call.

On a large result set it may take some time, but navigation through a dataset is fast because all records are stored in a FireDAC internal data storage. For many DBMSs, such as Microsoft SQL Server, MySQL Server, Sybase SQL Anywhere:

  • It releases client result set buffer and allows to execute the next command returning result sets.
  • It gives maximum fetch performance. For example, the fast forward-only cursor is used on Microsoft SQL Server.

ResourceOptions.SilentMode 设置为True, 就不需要FDGUIxWaitCursor之类的东西了, 同样也不再会自动改变鼠标样式, 默认是需要的

Controls the dataset GUI feedback.

Use the SilentMode property to control whether the dataset shows the SQL-hourglass mouse cursor when the dataset performs potentially long running actions. The dataset will show the SQL-hourglass if it is set to False. Otherwise, it is set to True. By default, it is set to False. For TFDCustomMemTable, it is set to True.

Setting SilentMode to True allows you to speed up the dataset processing algorithms.

TFDQuery:

ResourceOptions.DirectExecute 设置为True, 直接执行SQL, 默认是False, 在某些数据库(比如Postgre) 如果设置为False那么在执行多条SQL的时候会报错....

Controls the preparation of SQL statements.

Use the DirectExecute property to specify whether FireDAC should prepare an SQL statement before its execution (False) or execute it directly (True). The default value is False.

This property was introduced at first for MS SQL Server support. The prepared and the direct execution may have different effects, and even the prepared execution may fail. If you will get peculiar errors, then try to set DirectExecute to True.

FetchOptions.AutoClose := False; 不要自动关闭连接...这个如果不设置的话, 多结果集返回的时候会出问题....

Controls the automatic closing of a dataset cursor after fetching the last record.

Use the AutoClose property value to control the automatic closing of a dataset cursor.  If AutoCloseis set to True and the last record is fetched from the current result set, then the underlying cursor is closed, all additional result sets that have not been yet processed are discarded, and the resources associated with the cursors are released. The dataset (TFDDataSet descendant) itself is closed, but its internal command object is closed. This allows the release of DBMS resources associated with a cursor as fast as possible.

If your SQL command returns several result sets and you need to process all of them, then you must set AutoClose to False. Otherwise, after processing the first result set, all other result sets are discarded.

The default value is True.

 FDPhysMSSQLDriverLink:

ODBCDriver 连接SQL Server的驱动, 如果系统里有多个版本的SQL Server, 默认会使用最高版本的那个

    ODBCDriver := FindBestDriver([C_2012_NC, C_2012_ODBC, C_2008, C_2005, C_2000])

但是, 如果你要使用的数据库是2000, 而机器里有更高版本的数据库, 那么使用Native连接2000的时候就会出现各种奇怪的问题(比如无法同时edit2个超过1024k的二进制字段等)

所以如果有可能, 最好在创建连接的时候指定ODBCDriver