在行中设置数据 CRowset::SetData 在当前行的一个或多个列中设置数据值。以下代码设置绑定到 Products 表的“Name”和“Units in Stock”列的数据成员的值,然后调用 SetData 将这些值写入行集合的第 100 行: // Instantiate a rowset based on the user record class CTable<CAccessor<CProductAccessor> > product; CSession session;// Open the rowset and move to the 100th row product.Open(session, "Product", &ps, 1); // ps is the property set product.MoveToBook(&book, 0); // Assume that book is set to 100th row// Change the values of columns "Name" and "Units in Stock" in the current row of the Product table _tcscpy( product.m_ProductName, _T( "Candle" ) ); product.m_UnitsInStock = 10000;// Set the data HRESULT hr = product.SetData( ); 向行集合中插入行 CRowset::Insert 使用访问器中的数据创建并初始化新行。Insert 在当前行的后面创建一个全新的行;需要指定是将当前行递增到下一行还是保持当前行不变。可以通过设置 bGetRow 参数完成此操作: HRESULT Insert(int nAccessor = 0, bool bGetRow = false) false(默认值)指定当前行递增到下一行(在这种情况下它将指向被插入的行)。 true 指定当前行保持原来位置。 以下代码设置绑定到 Products 表的列的数据成员的值,然后调用 Insert 将一个具有这些值的新行插入到该行集合的第 100 行后面。建议设置所有列值以避免在新行中出现未定义的数据: // Instantiate a rowset based on the user record class CTable<CAccessor<CProductAccessor> > product; CSession session;// Open the rowset and move to the 100th row product.Open(session, "Product", &ps, 1); // ps is the property set product.MoveToBook(&book, 0); // Assume that book is set to 100th row// Set the column values for a row of the Product table, then insert the row product.m_ProductID = 101; _tcscpy( product.m_ProductName, _T( "Candle" ) ); product.m_SupplierID = 27857; product.m_CategoryID = 372; _tcscpy( product.m_QuantityPerUnit, _T( "Pack of 10" ) ); product.m_UnitPrice = 20; product.m_UnitsInStock = 10000; product.m_UnitsOnOrder = 5201; product.m_ReorderLevel = 5000; product.m_Discontinued = false;// You must also initialize the status and length fields before setting/inserting data // Set the column status values m_dwProductIDStatus = DBSTATUS_S_ISNULL; m_dwProductNameStatus = DBSTATUS_S_ISNULL; m_dwSupplierIDStatus = DBSTATUS_S_ISNULL; m_dwCategoryIDStatus = DBSTATUS_S_ISNULL; m_dwQuantityPerUnitStatus = DBSTATUS_S_ISNULL; m_dwUnitPriceStatus = DBSTATUS_S_ISNULL; m_dwUnitsInStockStatus = DBSTATUS_S_ISNULL; m_dwUnitsOnOrderStatus = DBSTATUS_S_ISNULL; m_dwReorderLevelStatus = DBSTATUS_S_ISNULL; m_dwDiscontinuedStatus = DBSTATUS_S_ISNULL;// Set the column length value for column data members that are not fixed-length types. // The value should be the length of the string that you are setting. m_dwProductNameLength = 6; // "Candle" has 6 characters m_dwQuantityPerUnitLength = 10; // "Pack of 10" has 10 characters// Insert the data HRESULT hr = product.Insert( ); 有关更详细的示例,请参见 CRowset::Insert。 有关设置状态和长度数据成员的更多信息,请参见向导生成的访问器中的字段状态数据成员。 从行集合中删除行 CRowset::Delete 从行集合中删除当前行。以下代码调用 Delete 以移除行集合的第 100 行: // Instantiate a rowset based on the user record class CTable<CAccessor<CProductAccessor> > product; CSession session;// Open the rowset and move to the 100th row product.Open(session, "Product", &ps, 1); // ps is the property set product.MoveToBook(&book, 0); // Assume that book is set to 100th row// Delete the row HRESULT hr = product.Delete( );
CRowset::SetData 在当前行的一个或多个列中设置数据值。以下代码设置绑定到 Products 表的“Name”和“Units in Stock”列的数据成员的值,然后调用 SetData 将这些值写入行集合的第 100 行:
// Instantiate a rowset based on the user record class
CTable<CAccessor<CProductAccessor> > product;
CSession session;// Open the rowset and move to the 100th row
product.Open(session, "Product", &ps, 1); // ps is the property set
product.MoveToBook(&book, 0); // Assume that book is set to 100th row// Change the values of columns "Name" and "Units in Stock" in the current row of the Product table
_tcscpy( product.m_ProductName, _T( "Candle" ) );
product.m_UnitsInStock = 10000;// Set the data
HRESULT hr = product.SetData( );
向行集合中插入行
CRowset::Insert 使用访问器中的数据创建并初始化新行。Insert 在当前行的后面创建一个全新的行;需要指定是将当前行递增到下一行还是保持当前行不变。可以通过设置 bGetRow 参数完成此操作:
HRESULT Insert(int nAccessor = 0, bool bGetRow = false)
false(默认值)指定当前行递增到下一行(在这种情况下它将指向被插入的行)。
true 指定当前行保持原来位置。
以下代码设置绑定到 Products 表的列的数据成员的值,然后调用 Insert 将一个具有这些值的新行插入到该行集合的第 100 行后面。建议设置所有列值以避免在新行中出现未定义的数据:
// Instantiate a rowset based on the user record class
CTable<CAccessor<CProductAccessor> > product;
CSession session;// Open the rowset and move to the 100th row
product.Open(session, "Product", &ps, 1); // ps is the property set
product.MoveToBook(&book, 0); // Assume that book is set to 100th row// Set the column values for a row of the Product table, then insert the row
product.m_ProductID = 101;
_tcscpy( product.m_ProductName, _T( "Candle" ) );
product.m_SupplierID = 27857;
product.m_CategoryID = 372;
_tcscpy( product.m_QuantityPerUnit, _T( "Pack of 10" ) );
product.m_UnitPrice = 20;
product.m_UnitsInStock = 10000;
product.m_UnitsOnOrder = 5201;
product.m_ReorderLevel = 5000;
product.m_Discontinued = false;// You must also initialize the status and length fields before setting/inserting data
// Set the column status values
m_dwProductIDStatus = DBSTATUS_S_ISNULL;
m_dwProductNameStatus = DBSTATUS_S_ISNULL;
m_dwSupplierIDStatus = DBSTATUS_S_ISNULL;
m_dwCategoryIDStatus = DBSTATUS_S_ISNULL;
m_dwQuantityPerUnitStatus = DBSTATUS_S_ISNULL;
m_dwUnitPriceStatus = DBSTATUS_S_ISNULL;
m_dwUnitsInStockStatus = DBSTATUS_S_ISNULL;
m_dwUnitsOnOrderStatus = DBSTATUS_S_ISNULL;
m_dwReorderLevelStatus = DBSTATUS_S_ISNULL;
m_dwDiscontinuedStatus = DBSTATUS_S_ISNULL;// Set the column length value for column data members that are not fixed-length types.
// The value should be the length of the string that you are setting.
m_dwProductNameLength = 6; // "Candle" has 6 characters
m_dwQuantityPerUnitLength = 10; // "Pack of 10" has 10 characters// Insert the data
HRESULT hr = product.Insert( );
有关更详细的示例,请参见 CRowset::Insert。
有关设置状态和长度数据成员的更多信息,请参见向导生成的访问器中的字段状态数据成员。
从行集合中删除行
CRowset::Delete 从行集合中删除当前行。以下代码调用 Delete 以移除行集合的第 100 行:
// Instantiate a rowset based on the user record class
CTable<CAccessor<CProductAccessor> > product;
CSession session;// Open the rowset and move to the 100th row
product.Open(session, "Product", &ps, 1); // ps is the property set
product.MoveToBook(&book, 0); // Assume that book is set to 100th row// Delete the row
HRESULT hr = product.Delete( );