使用 SQLSetPos 更新行集中的資料列

SQLSetPos 的更新操作會使用應用程式緩衝區中綁定欄位的資料,更新資料來源表格中一個或多個選定的列(除非長度/指示器緩衝區中的值為 SQL_COLUMN_IGNORE)。 未綁定的欄位將不會被更新。

要用 SQLSetPos 更新資料列,應用程式會做以下操作:

  1. 將新的資料值放入列集緩衝區。 關於如何用 SQLSetPos 傳送長資料的資訊,請參見 Long Data、SQLSetPos 和 SQLBulkOperations

  2. 根據需要設定每欄長度/指示緩衝區的值。 這是綁定到字串緩衝區的欄位的資料位元組長度或 SQL_NTS,綁定到二進位緩衝區的欄位的資料位元組長度,對於任何要設定為 NULL 的欄位則為 SQL_NULL_DATA。

  3. 設定那些不需更新為SQL_COLUMN_IGNORE欄位的長度/指示緩衝區值。 雖然應用程式可以跳過此步驟並重新傳送現有資料,但這效率低落,且有可能將讀取時被截斷的數值傳送到資料來源。

  4. 呼叫 SQLSetPos時, 操作設為 SQL_UPDATE,RowNumber 設為要更新的列號。 若 RowNumber 為 0,則行集中的所有列都會被更新。

SQLSetPos 回傳後,當前列會被設定為更新後的列。

當更新列集的所有列(RowNumber 等於 0)時,應用程式可以透過將列操作陣列中對應元素(由 SQL_ATTR_ROW_OPERATION_PTR 陳述句屬性指向)設定為 SQL_ROW_IGNORE,來停用某些列的更新。 列操作陣列的大小與元素數量對應列狀態陣列(由 SQL_ATTR_ROW_STATUS_PTR 陳述屬性指向)。 為了只更新結果集中那些成功擷取且尚未從行集中刪除的列,應用程式會使用用於擷取行集的函式中的行狀態陣列,並作為行操作陣列傳遞給SQLSetPos

當每一列被作為更新傳送至資料來源時,應用程式的緩衝區應該有有效的資料列資料。 如果應用程式的緩衝區是透過擷取填滿,且有維護列狀態陣列,則這些列位置的值應該不會是SQL_ROW_DELETED、SQL_ROW_ERROR或SQL_ROW_NOROW。

例如,以下程式碼允許使用者捲動客戶資料表並更新、刪除或新增資料列。 它會先將新資料放入列集緩衝區,然後再呼叫 SQLSetPos 來更新或新增列。 在行集緩衝區末端會多分配一列用來存放新列;這可防止在緩衝區放置新資料列時,現有資料被覆寫。

#define UPDATE_ROW   100  
#define DELETE_ROW   101  
#define ADD_ROW      102  
  
SQLUINTEGER    CustIDArray[11];  
SQLCHAR        NameArray[11][51], AddressArray[11][51],   
               PhoneArray[11][11];  
SQLINTEGER     CustIDIndArray[11], NameLenOrIndArray[11],   
               AddressLenOrIndArray[11],  
               PhoneLenOrIndArray[11];  
SQLUSMALLINT   RowStatusArray[10], Action, RowNum;  
SQLRETURN      rc;  
SQLHSTMT       hstmt;  
  
// Set the SQL_ATTR_ROW_BIND_TYPE statement attribute to use column-wise   
// binding. 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.  
SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_KEYSET_DRIVEN, 0);  
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);  
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, 10, 0);  
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_STATUS_PTR, RowStatusArray, 0);  
  
// Bind arrays to the CustID, Name, Address, and Phone columns.  
SQLBindCol(hstmt, 1, SQL_C_ULONG, CustIDArray, 0, CustIDIndArray);  
SQLBindCol(hstmt, 2, SQL_C_CHAR, NameArray, sizeof(NameArray[0]), NameLenOrIndArray);  
SQLBindCol(hstmt, 3, SQL_C_CHAR, AddressArray, sizeof(AddressArray[0]),  
            AddressLenOrIndArray);  
SQLBindCol(hstmt, 4, SQL_C_CHAR, PhoneArray, sizeof(PhoneArray[0]),  
            PhoneLenOrIndArray);  
  
// Execute a statement to retrieve rows from the Customers table.  
SQLExecDirect(hstmt, "SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);  
  
// Fetch and display the first 10 rows.  
rc = SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 0);  
DisplayData(CustIDArray, CustIDIndArray, NameArray, NameLenOrIndArray, AddressArray,  
            AddressLenOrIndArray, PhoneArray, PhoneLenOrIndArray, RowStatusArray);  
  
// Call GetAction to get an action and a row number from the user.  
while (GetAction(&Action, &RowNum)) {  
   switch (Action) {  
  
      case SQL_FETCH_NEXT:  
      case SQL_FETCH_PRIOR:  
      case SQL_FETCH_FIRST:  
      case SQL_FETCH_LAST:  
      case SQL_FETCH_ABSOLUTE:  
      case SQL_FETCH_RELATIVE:  
         // Fetch and display the requested data.  
         SQLFetchScroll(hstmt, Action, RowNum);  
         DisplayData(CustIDArray, CustIDIndArray,  
                     NameArray, NameLenOrIndArray,  
                     AddressArray, AddressLenOrIndArray,  
                     PhoneArray, PhoneLenOrIndArray, RowStatusArray);  
         break;  
  
      case UPDATE_ROW:  
         // Place the new data in the rowset buffers and update the   
         // specified row.  
         GetNewData(&CustIDArray[RowNum - 1], &CustIDIndArray[RowNum - 1],  
                  NameArray[RowNum - 1], &NameLenOrIndArray[RowNum - 1],  
                  AddressArray[RowNum - 1], &AddressLenOrIndArray[RowNum - 1],  
                  PhoneArray[RowNum - 1], &PhoneLenOrIndArray[RowNum - 1]);  
         SQLSetPos(hstmt, RowNum, SQL_UPDATE, SQL_LOCK_NO_CHANGE);  
         break;  
  
      case DELETE_ROW:  
         // Delete the specified row.  
         SQLSetPos(hstmt, RowNum, SQL_DELETE, SQL_LOCK_NO_CHANGE);  
         break;  
  
      case ADD_ROW:  
         // Place the new data in the rowset buffers at index 10.   
         // This is an extra element for new rows so rowset data is   
         // not overwritten. Insert the new row. Row 11 corresponds   
         // to index 10.  
         GetNewData(&CustIDArray[10], &CustIDIndArray[10],  
                     NameArray[10], &NameLenOrIndArray[10],  
                     AddressArray[10], &AddressLenOrIndArray[10],  
                     PhoneArray[10], &PhoneLenOrIndArray[10]);  
         SQLSetPos(hstmt, 11, SQL_ADD, SQL_LOCK_NO_CHANGE);  
         break;  
   }  
}  
  
// Close the cursor.  
SQLCloseCursor(hstmt);