游标Cursors

应用程序提取使用数据游标An application fetches data with a cursor. 光标位于不同于结果集:结果集是与特定搜索条件匹配的行集,而游标是返回的行应用程序的软件。A cursor is different from a result set: A result set is the set of rows that matches particular search criteria, whereas a cursor is the software that returns those rows to the application. 名称游标, 当其应用到数据库,可能源自终端的计算机上闪烁的光标。The name cursor, as it applies to databases, probably originated from the blinking cursor on a computer terminal. 就像该光标指示当前位置在屏幕上键入的词语将显示下一步,结果集的游标指示结果集和接下来将返回哪些行中的当前位置。Just as that cursor indicates the current position on the screen and where the typed words will appear next, a cursor on a result set indicates the current position in the result set and what row will be returned next.

ODBC 中的游标模型基于的嵌入式 SQL 中的游标模型。The cursor model in ODBC is based on the cursor model in embedded SQL. 这些模型之间的一个显著区别是方式游标已打开。One notable difference between these models is the way cursors are opened. 在嵌入式 SQL 中,游标必须是显式声明和打开之前可以使用它。In embedded SQL, a cursor must be explicitly declared and opened before it can be used. 在 ODBC 中,创建结果集的语句执行时隐式打开游标。In ODBC, a cursor is implicitly opened when a statement that creates a result set is executed. 打开游标时,它被定位在结果集中的第一行之前。When the cursor is opened, it is positioned before the first row of the result set. 在嵌入 SQL 和 ODBC,应用程序使用完后,必须关闭游标。In both embedded SQL and ODBC, a cursor must be closed after the application has finished using it.

不同的游标有不同的特征。Different cursors have different characteristics. 最常见的游标类型,称为只进游标结果集可以仅向前移动。The most common type of cursor, which is called a forward-only cursor, can only move forward through the result set. 若要返回上一行,应用程序必须关闭并重新打开游标,然后从结果集直到它达到所需的行的开头读取行。To return to a previous row, the application must close and reopen the cursor and then read rows from the beginning of the result set until it reaches the required row. 只进游标提供快速机制可使经过结果集一次。Forward-only cursors provide a fast mechanism for making a single pass through a result set.

只进游标是不太适用于基于屏幕的应用程序,在用户滚动向后和向前浏览数据。Forward-only cursors are less useful for screen-based applications, in which the user scrolls backward and forward through the data. 此类应用程序可以通过读取结果集后,缓存数据保存在本地,并执行滚动本身使用只进游标。Such applications can use a forward-only cursor by reading the result set once, caching the data locally, and performing scrolling themselves. 但是,这也仅适用于较少的数据。However, this works well only with small amounts of data. 更好的解决方案是使用可滚动游标, 以便进行随机访问的结果集。A better solution is to use a scrollable cursor, which provides random access to the result set. 此类应用程序还可以提高性能的一次提取多行数据使用的名称是什么块游标。Such applications can also increase performance by fetching more than one row of data at a time, using what is called a block cursor. 有关块游标的详细信息,请参阅使用块状游标For more information about block cursors, see Using Block Cursors.

只进游标是在 ODBC 中的默认游标类型和以下各节中讨论。The forward-only cursor is the default cursor type in ODBC and is discussed in the following sections. 有关块游标和可滚动游标的详细信息,请参阅块状游标可滚动游标For more information about block cursors and scrollable cursors, see Block Cursors and Scrollable Cursors.

重要

提交或回滚事务,通过显式调用SQLEndTran或通过在自动提交模式下操作时,会导致某些数据源,以关闭上一个连接上的所有语句的所有游标。Committing or rolling back a transaction, either by explicitly calling SQLEndTran or by operating in auto-commit mode, causes some data sources to close all the cursors on all statements on a connection. 有关详细信息,请参阅中的属性 SQL_CURSOR_COMMIT_BEHAVIOR 和 SQL_CURSOR_ROLLBACK_BEHAVIOR SQLGetInfo函数说明。For more information, see the SQL_CURSOR_COMMIT_BEHAVIOR and SQL_CURSOR_ROLLBACK_BEHAVIOR attributes in the SQLGetInfo function description.