Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 260900 - Last Review: July 1, 2004 - Revision: 3.2
How To Insert NULL Data with OLE DB Consumer Templates
This article was previously published under Q260900
This article demonstrates how to use OLE DB consumer templates to insert NULL data into an SQL Server table by using a parameterized query.
When you use the OLE DB consumer templates to insert NULL data, you must set the status of the input parameter to DBSTATUS_S_ISNULL. This sends the NULL value to the provider, and lets the provider ignore the contents of the value and length portions of the consumer buffer.
The following sample code is an OLE DB consumer application that uses OLE DB consumer template classes to insert a NULL value into a column by using a parameterized query. The application then retrieves all records in the table to verify that the NULL value has been inserted.
To create and run this sample program, perform the following steps:
- Use the following script to create a table called "TestTable":
CREATE TABLE TestTable (
ID int IDENTITY (1, 1) NOT NULL Primary Key,
Name char (10) NULL
)
GO
- Paste the following code in a new Console Application in Visual C++ 6.0:
#define _ATL_STATIC_REGISTRY
#define _ATL_DEBUG_QI
#define _ATL_DEBUG_INTERFACES
#include <atldbcli.h>
#include <iostream.h>
#define RETURNHR(hr) if(FAILED((HRESULT)hr)) { AtlTraceErrorRecords((HRESULT)hr); return E_FAIL; }
// The user record.
class CTestNullParamInAccessor
{
public:
//Parameter variable.
CHAR m_InParam[2];
ULONG m_status;
// Parameter binding.
BEGIN_PARAM_MAP(CTestNullParamInAccessor)
SET_PARAM_TYPE(DBPARAMIO_INPUT)
COLUMN_ENTRY_STATUS(1,m_InParam,m_status)
END_PARAM_MAP()
};
class CTestOutputAccessor
{
public:
// Data elements.
CHAR m_Name[10];
ULONG m_status;
// Output binding.
BEGIN_COLUMN_MAP(CTestOutputAccessor)
COLUMN_ENTRY_STATUS(1,m_Name, m_status)
END_COLUMN_MAP()
};
int main(void)
{
HRESULT hr;
CDataSource connection;
CSession session;
CCommand<CAccessor< CTestNullParamInAccessor >,CNoRowset> cmdTest;
// Connect the database, session, and accessors.
CoInitialize(NULL);
CDBPropSet dbinit(DBPROPSET_DBINIT);
dbinit.AddProperty(DBPROP_INIT_DATASOURCE, OLESTR("srv"));
dbinit.AddProperty(DBPROP_AUTH_USERID, OLESTR("user_id"));
dbinit.AddProperty(DBPROP_AUTH_PASSWORD, OLESTR("password"));
dbinit.AddProperty(DBPROP_INIT_CATALOG, OLESTR("database_name"));
dbinit.AddProperty(DBPROP_INIT_MODE, (long) DB_MODE_READWRITE);
dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
RETURNHR(hr = connection.Open("SQLOLEDB.1",&dbinit))
// Create the session.
RETURNHR(hr = session.Open(connection))
// Insert a record with a "NULL" value on the "name" field.
// DBSTATUS_S_ISNULL - the OLE DB provider uses a null value
// and proceed to next input parameter if there is one.
cmdTest.m_status = DBSTATUS_S_ISNULL;
// Open call executes the insert statement.
RETURNHR(hr = cmdTest.Open(session, "Insert Into TestTable (Name) Values (?)"))
cmdTest.Close();
// Retrieve the data.
CCommand<CAccessor< CTestOutputAccessor > > cmdTest2;
RETURNHR(hr = cmdTest2.Open(session, "Select Name from TestTable"))
RETURNHR(hr = cmdTest2.MoveFirst())
cout << "NULL Param Insertion Demo" << endl;
cout << "=========================\n" << endl;
long i=1;
do
{
cout << "(" << i++ << ")";
if (cmdTest2.m_status == DBSTATUS_S_OK)
cout << cmdTest2.m_Name<< endl;
else if (cmdTest2.m_status == DBSTATUS_S_ISNULL)
cout << "NULL" << endl;
else
cout << "ERROR" << endl;
}while (cmdTest2.MoveNext() == S_OK);
cmdTest2.Close();
session.Close( );
connection.Close();
return S_OK;
}
- Compile and run the application.
For additional information on OLE DB consumer templates, refer to the Platform SDK documentation.
For additional information on adding NULL data to SQL tables, click the article numbers below
to view the articles in the Microsoft Knowledge Base:
260310Â
(http://kbalertz.com/Feedback.aspx?kbNumber=260310/EN-US/
)
How To Insert NULL Data with OLEDB SDK Interfaces
248799Â
(http://kbalertz.com/Feedback.aspx?kbNumber=248799/EN-US/
)
How To Insert NULL Data with ODBC API Functions
APPLIES TO
- Microsoft OLE DB Provider for SQL Server 7.0
- Microsoft OLE DB Provider for SQL Server 7.01
- Microsoft ODBC Driver for Microsoft SQL Server 3.7
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft SQL Server 7.0 Standard Edition
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate