應用程式透過呼叫 SQLBindCol 來綁定欄位。 此函式一次綁定一欄。 藉此,應用程式會指定以下內容:
欄位編號。 第 0 欄是書籤欄;此欄位未包含在某些結果集中。 其他欄位皆以數字1開始編號。 綁定比結果集中列數更高的欄位會造成錯誤;此錯誤直到結果集建立後才可偵測,因此由 SQLFetch 回傳,而非 SQLBindCol。
綁定於欄位的變數的 C 資料型態、位址與位元組長度。 若指定 C 資料型別,而該欄位的 SQL 資料型別無法轉換,則屬錯誤;此錯誤可能要等到結果集建立後才會被偵測,因此由 SQLFetch 回傳,而非 SQLBindCol。 有關支援的轉換清單,請參閱附錄D:資料型別中的 「將資料從SQL轉換為C資料型別 」。 關於位元組長度的資訊,請參見 資料緩衝區長度。
長度/指示緩衝區的位址。 長度/指示緩衝區是可選的。 它用來回傳二進位或字元資料的位元組長度,或在資料為 NULL 時回傳 SQL_NULL_DATA。 欲了解更多資訊,請參閱 使用長度/指示值。
當呼叫 SQLBindCol 時,驅動程式會將此資訊與該語句關聯起來。 當每一列資料被擷取時,會利用這些資訊將每一欄的資料放入綁定的應用程式變數中。
例如,以下程式碼將變數綁定到 SalesPerson 和 CustID 欄位。 欄位資料會以 SalesPerson 和 CustID 回傳。 由於 SalesPerson 是字元緩衝區,應用程式會指定其位元組長度(11),讓驅動程式判斷是否要截斷資料。 回傳標題的位元組長度,或是否為 NULL,會在 SalesPersonLenOrInd 中回傳。
由於 CustID 是整數變數且長度固定,因此無需指定其位元組長度;驅動程式假設它是 sizeof(SQLUINTEGER)。 回傳的客戶 ID 資料的位元組長度,或是否為 NULL,會在 CustIDInd 中回傳。 請注意,應用程式只關心薪資是否為 NULL,因為位元組長度總是 sizeof(SQLUINTEGER)。
SQLCHAR SalesPerson[11];
SQLUINTEGER CustID;
SQLINTEGER SalesPersonLenOrInd, CustIDInd;
SQLRETURN rc;
SQLHSTMT hstmt;
// Bind SalesPerson to the SalesPerson column and CustID to the
// CustID column.
SQLBindCol(hstmt, 1, SQL_C_CHAR, SalesPerson, sizeof(SalesPerson),
&SalesPersonLenOrInd);
SQLBindCol(hstmt, 2, SQL_C_ULONG, &CustID, 0, &CustIDInd);
// Execute a statement to get the sales person/customer of all orders.
SQLExecDirect(hstmt, "SELECT SalesPerson, CustID FROM Orders ORDER BY SalesPerson",
SQL_NTS);
// Fetch and print the data. Print "NULL" if the data is NULL. Code to
// check if rc equals SQL_ERROR or SQL_SUCCESS_WITH_INFO not shown.
while ((rc = SQLFetch(hstmt)) != SQL_NO_DATA) {
if (SalesPersonLenOrInd == SQL_NULL_DATA)
printf("NULL ");
else
printf("%10s ", SalesPerson);
if (CustIDInd == SQL_NULL_DATA)
printf("NULL\n");
else
printf("%d\n", CustID);
}
// Close the cursor.
SQLCloseCursor(hstmt);
以下程式碼執行使用者輸入的 SELECT 語句,並列印結果集中的每一列資料。 由於應用程式無法預測由 SELECT 陳述式所產生的結果集形狀,因此無法像前述範例中那樣,將硬編碼變數綁定到結果集。 應用程式會為該列的每一欄分配一個緩衝區來存放資料,以及一個長度/指示緩衝區。 對於每一欄,它會計算該欄記憶體起始點的偏移量,並調整此偏移量,使該欄的資料及長度/指示緩衝區從對齊邊界開始。 接著它會將從偏移位置開始的記憶體綁定到列。 從驅動程式的角度來看,這個記憶體的位址與前述變數綁定的位址無法區分。 欲了解更多關於對齊的資訊,請參見對齊。
// This application allocates a buffer at run time. For each column, this
// buffer contains memory for the column's data and length/indicator.
// For example:
// column 1 column 2 column 3 column 4
// <------------><---------------><-----><------------>
// db1 li1 db2 li2 db3 li3 db4 li4
// | | | | | | | |
// _____V_____V________V_______V___V___V______V_____V_
// |__________|__|_____________|__|___|__|__________|__|
//
// dbn = data buffer for column n
// lin = length/indicator buffer for column n
// Define a macro to increase the size of a buffer so that it is a
// multiple of the alignment size. Thus, if a buffer starts on an
// alignment boundary, it will end just before the next alignment
// boundary. In this example, an alignment size of 4 is used because
// this is the size of the largest data type used in the application's
// buffer--the size of an SDWORD and of the largest default C data type
// are both 4. If a larger data type (such as _int64) was used, it would
// be necessary to align for that size.
#define ALIGNSIZE 4
#define ALIGNBUF(Length) Length % ALIGNSIZE ? \
Length + ALIGNSIZE - (Length % ALIGNSIZE) : Length
SQLCHAR SelectStmt[100];
SQLSMALLINT NumCols, *CTypeArray, i;
SQLINTEGER * ColLenArray, *OffsetArray, SQLType, *DataPtr;
SQLRETURN rc;
SQLHSTMT hstmt;
// Get a SELECT statement from the user and execute it.
GetSelectStmt(SelectStmt, 100);
SQLExecDirect(hstmt, SelectStmt, SQL_NTS);
// Determine the number of result set columns. Allocate arrays to hold
// the C type, byte length, and buffer offset to the data.
SQLNumResultCols(hstmt, &NumCols);
CTypeArray = (SQLSMALLINT *) malloc(NumCols * sizeof(SQLSMALLINT));
ColLenArray = (SQLINTEGER *) malloc(NumCols * sizeof(SQLINTEGER));
OffsetArray = (SQLINTEGER *) malloc(NumCols * sizeof(SQLINTEGER));
OffsetArray[0] = 0;
for (i = 0; i < NumCols; i++) {
// Determine the column's SQL type. GetDefaultCType contains a switch
// statement that returns the default C type for each SQL type.
SQLColAttribute(hstmt, ((SQLUSMALLINT) i) + 1, SQL_DESC_TYPE, NULL, 0, NULL, (SQLPOINTER) &SQLType);
CTypeArray[i] = GetDefaultCType(SQLType);
// Determine the column's byte length. Calculate the offset in the
// buffer to the data as the offset to the previous column, plus the
// byte length of the previous column, plus the byte length of the
// previous column's length/indicator buffer. Note that the byte
// length of the column and the length/indicator buffer are increased
// so that, assuming they start on an alignment boundary, they will
// end on the byte before the next alignment boundary. Although this
// might leave some holes in the buffer, it is a relatively
// inexpensive way to guarantee alignment.
SQLColAttribute(hstmt, ((SQLUSMALLINT) i)+1, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, &ColLenArray[i]);
ColLenArray[i] = ALIGNBUF(ColLenArray[i]);
if (i)
OffsetArray[i] = OffsetArray[i-1]+ColLenArray[i-1]+ALIGNBUF(sizeof(SQLINTEGER));
}
// Allocate the data buffer. The size of the buffer is equal to the
// offset to the data buffer for the final column, plus the byte length
// of the data buffer and length/indicator buffer for the last column.
void *DataPtr = malloc(OffsetArray[NumCols - 1] +
ColLenArray[NumCols - 1] + ALIGNBUF(sizeof(SQLINTEGER)));
// For each column, bind the address in the buffer at the start of the
// memory allocated for that column's data and the address at the start
// of the memory allocated for that column's length/indicator buffer.
for (i = 0; i < NumCols; i++)
SQLBindCol(hstmt,
((SQLUSMALLINT) i) + 1,
CTypeArray[i],
(SQLPOINTER)((SQLCHAR *)DataPtr + OffsetArray[i]),
ColLenArray[i],
(SQLINTEGER *)((SQLCHAR *)DataPtr + OffsetArray[i] + ColLenArray[i]));
// Retrieve and print each row. PrintData accepts a pointer to the data,
// its C type, and its byte length/indicator. It contains a switch
// statement that casts and prints the data according to its type. Code
// to check if rc equals SQL_ERROR or SQL_SUCCESS_WITH_INFO not shown.
while ((rc = SQLFetch(hstmt)) != SQL_NO_DATA) {
for (i = 0; i < NumCols; i++) {
PrintData((SQLCHAR *)DataPtr[OffsetArray[i]], CTypeArray[i],
(SQLINTEGER *)((SQLCHAR *)DataPtr[OffsetArray[i] + ColLenArray[i]]));
}
}
// Close the cursor.
SQLCloseCursor(hstmt);