在使用列式綁定時,應用程式會為每個要回傳資料的欄位定義一個或兩個,甚至在某些情況下三個元素的結構。 第一個元素儲存資料值,第二個元素儲存長度/指示緩衝區。 指示符與長度值可透過將SQL_DESC_INDICATOR_PTR與SQL_DESC_OCTET_LENGTH_PTR描述符欄位設定為不同值,分別儲存在不同的緩衝區;若如此,結構中包含第三個元素。 應用程式接著分配一個這些結構的陣列,該陣列包含與列集中列數相同的元素。
應用程式會用 SQL_ATTR_ROW_BIND_TYPE 陳述句屬性宣告結構體大小到驅動程式,並綁定陣列第一個元素中每個成員的位址。 因此,驅動程式可計算特定列與欄的資料位址為
Address = Bound Address + ((Row Number - 1) * Structure Size)
其中行的編號從 1 到行集的大小。 (列號減一,因為 C 中的陣列索引是從零開始的。)下圖示範了按列順序綁定的工作方式。 通常,結構中只包含將被綁定的欄位。 結構可包含與結果集欄位無關的欄位。 欄可以任意順序放置於結構中,但為了清晰起見,會依序排列。
例如,以下程式碼建立了一個結構,其中包含用於返回 OrderID、SalesPerson 和 Status 欄位資料的元素,並設有 SalesPerson 和 Status 欄位的長度/指標。 它分配了 10 個結構,並將其綁定到 OrderID、SalesPerson 和 Status 欄位。
#define ROW_ARRAY_SIZE 10
// Define the ORDERINFO struct and allocate an array of 10 structs.
typedef struct {
SQLUINTEGER OrderID;
SQLINTEGER OrderIDInd;
SQLCHAR SalesPerson[11];
SQLINTEGER SalesPersonLenOrInd;
SQLCHAR Status[7];
SQLINTEGER StatusLenOrInd;
} ORDERINFO;
ORDERINFO OrderInfoArray[ROW_ARRAY_SIZE];
SQLULEN NumRowsFetched;
SQLUSMALLINT RowStatusArray[ROW_ARRAY_SIZE], i;
SQLRETURN rc;
SQLHSTMT hstmt;
// Specify the size of the structure with the SQL_ATTR_ROW_BIND_TYPE
// statement attribute. This also declares that row-wise binding will
// be used. Declare the rowset size with the SQL_ATTR_ROW_ARRAY_SIZE
// statement attribute. Set the SQL_ATTR_ROW_STATUS_PTR statement
// attribute to point to the row status array. Set the
// SQL_ATTR_ROWS_FETCHED_PTR statement attribute to point to
// NumRowsFetched.
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, sizeof(ORDERINFO), 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, ROW_ARRAY_SIZE, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_STATUS_PTR, RowStatusArray, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &NumRowsFetched, 0);
// Bind elements of the first structure in the array to the OrderID,
// SalesPerson, and Status columns.
SQLBindCol(hstmt, 1, SQL_C_ULONG, &OrderInfoArray[0].OrderID, 0, &OrderInfoArray[0].OrderIDInd);
SQLBindCol(hstmt, 2, SQL_C_CHAR, OrderInfoArray[0].SalesPerson,
sizeof(OrderInfoArray[0].SalesPerson),
&OrderInfoArray[0].SalesPersonLenOrInd);
SQLBindCol(hstmt, 3, SQL_C_CHAR, OrderInfoArray[0].Status,
sizeof(OrderInfoArray[0].Status), &OrderInfoArray[0].StatusLenOrInd);
// Execute a statement to retrieve rows from the Orders table.
SQLExecDirect(hstmt, "SELECT OrderID, SalesPerson, Status FROM Orders", SQL_NTS);
// Fetch up to the rowset size number of rows at a time. Print the actual
// number of rows fetched; this number is returned in NumRowsFetched.
// Check the row status array to print only those rows successfully
// fetched. Code to check if rc equals SQL_SUCCESS_WITH_INFO or
// SQL_ERRORnot shown.
while ((rc = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0)) != SQL_NO_DATA) {
for (i = 0; i < NumRowsFetched; i++) {
if (RowStatusArray[i] == SQL_ROW_SUCCESS|| RowStatusArray[i] ==
SQL_ROW_SUCCESS_WITH_INFO) {
if (OrderInfoArray[i].OrderIDInd == SQL_NULL_DATA)
printf(" NULL ");
else
printf("%d\t", OrderInfoArray[i].OrderID);
if (OrderInfoArray[i].SalesPersonLenOrInd == SQL_NULL_DATA)
printf(" NULL ");
else
printf("%s\t", OrderInfoArray[i].SalesPerson);
if (OrderInfoArray[i].StatusLenOrInd == SQL_NULL_DATA)
printf(" NULL\n");
else
printf("%s\n", OrderInfoArray[i].Status);
}
}
}
// Close the cursor.
SQLCloseCursor(hstmt);