Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to use SQL Server identity, OLE DB templates, and OLE DB for ODBC in Visual C++


View products that this article applies to.

This article was previously published under Q194678
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

↑ Back to the top


Summary

When using the OLE DB Templates with a SQL Server database, it is often necessary to add records to a table that contains an identity column.

Getting an identity column to increment properly with the OLE DB for ODBC provider that ships with Visual C++ 6.0, Msdasql.dll, requires that the COLUMN_ENTRY_STATUS macro be used.

↑ Back to the top


More information

The following sample code demonstrates how to use this macro when defining an accessor map with Visual C++ 6.0 OLE DB Consumer Templates:

Sample Code

   class CMyTableAccessor
   {
   public:
      LONG m_id;          // This is an identity column
      DWORD m_id_status;  // Status variable for id column
      TCHAR m_name[11];

   BEGIN_COLUMN_MAP(CMyTableAccessor)
     COLUMN_ENTRY_STATUS(1, m_id, m_id_status)
     COLUMN_ENTRY(2, m_name)
   END_COLUMN_MAP()

   ...
   };
				
The code to add a new record to the table would resemble the following:
   CMyTable rs;

   rs.Open();
   rs.ClearRecord();  //Null out current structure

   strcpy(rs.m_name , "New Name");
   rs.m_id_status = DBSTATUS_S_IGNORE;  //Tells the provider to ignore this
                                        // column when updating.

   rs.Insert();  // Insert new record into the table letting server update
                // of the identity column.
   rs.Close();
				

↑ Back to the top


References

Please see the following topics in MSDN online documentation:

  • VC++ documentation for COLUMN_ENTRY_STATUS
  • OLE DB Topic titled "Status" defines "DBSTATUS_S_IGNORE"

↑ Back to the top


Keywords: KB194678, kbdatabase, kbconsumer, kbprovider, kbhowto

↑ Back to the top

Article Info
Article ID : 194678
Revision : 3
Created on : 1/11/2006
Published on : 1/11/2006
Exists online : False
Views : 593