Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should be setting a DBBINDING that includes DBPART_LENGTH to get the length of the field. To illustrate this, I've created some VARCHAR data in SQL Server using the following DDL:</p> <pre><code>CREATE TABLE NEWS ( ID INT NOT NULL, ARTICLE NVARCHAR(4000) ); INSERT INTO NEWS (1, 'Today is a sunny day.'); </code></pre> <p>Then I use the following ATL based C++ code sample to read the ARTICLE field. If you put breakpoints in the code, you will see it will come back with</p> <ul> <li>data.nArticleLength = 21</li> <li>data.szArticle = "Today is a sunny day."</li> </ul> <p>Here's the code sample sample:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;tchar.h&gt; #include &lt;windows.h&gt; #include &lt;oledb.h&gt; #include &lt;atlbase.h&gt; int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr = S_OK; hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); // Connect to SQL Server. CComPtr&lt;IDBInitialize&gt; spIDBInitialize; hr = spIDBInitialize.CoCreateInstance(OLESTR("SQLNCLI")); CComPtr&lt;IDBProperties&gt; spIDBProperties; hr = spIDBInitialize-&gt;QueryInterface(IID_IDBProperties, (void**) &amp;spIDBProperties); CComVariant varDataSource(OLESTR("InsertYourSQLServer")); CComVariant varCatalog(_T("InsertYourDatabase")); CComVariant varUserID(_T("InsertYourUserName")); CComVariant varPassword(_T("InsertYourPassword")); DBPROP rgProps[4] = { { DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, 0, DB_NULLID, varDataSource }, { DBPROP_INIT_CATALOG, DBPROPOPTIONS_REQUIRED, 0, DB_NULLID, varCatalog }, { DBPROP_AUTH_USERID, DBPROPOPTIONS_REQUIRED, 0, DB_NULLID, varUserID }, { DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_REQUIRED, 0, DB_NULLID, varPassword } }; DBPROPSET propSet = {rgProps, 4, DBPROPSET_DBINIT}; hr = spIDBProperties-&gt;SetProperties(1, &amp;propSet); spIDBProperties = NULL; hr = spIDBInitialize-&gt;Initialize(); // Execute the query. CComPtr&lt;IDBCreateSession&gt; spIDBCreateSession; hr = spIDBInitialize-&gt;QueryInterface(IID_IDBCreateSession, (void**) &amp;spIDBCreateSession); CComPtr&lt;IDBCreateCommand&gt; spIDBCreateCommand; hr = spIDBCreateSession-&gt;CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**) &amp;spIDBCreateCommand); spIDBCreateSession = NULL; CComPtr&lt;ICommandText&gt; spICommandText; hr = spIDBCreateCommand-&gt;CreateCommand(NULL, IID_ICommandText, (IUnknown**) &amp;spICommandText); spIDBCreateCommand = NULL; hr = spICommandText-&gt;SetCommandText(DBGUID_SQL, OLESTR("SELECT ID, ARTICLE FROM NEWS")); DBROWCOUNT cRowsAffected = 0; CComPtr&lt;IRowset&gt; spIRowset; hr = spICommandText-&gt;Execute(NULL, IID_IRowset, NULL, &amp;cRowsAffected, (IUnknown**) &amp;spIRowset); spICommandText = NULL; // Retrieve records. HROW hRow = NULL; HROW *rghRow = &amp;hRow; DBCOUNTITEM cRowsObtained = 0; hr = spIRowset-&gt;GetNextRows(DB_NULL_HCHAPTER, 0, 1, &amp;cRowsObtained, &amp;rghRow); while (hr == S_OK &amp;&amp; cRowsObtained == 1) { // Fetch the ARTICLE field. struct { DBSTATUS dbArticleStatus; ULONG nArticleLength; char szArticle[4000 + 4]; } data = {0}; DBBINDING Binding = {0}; Binding.iOrdinal = 2; Binding.obValue = sizeof(DBSTATUS) + sizeof(ULONG); Binding.obLength = sizeof(DBSTATUS); Binding.obStatus = 0; Binding.pTypeInfo = NULL; Binding.pObject = NULL; Binding.pBindExt = NULL; Binding.dwPart = DBPART_STATUS | DBPART_LENGTH | DBPART_VALUE; Binding.dwMemOwner = DBMEMOWNER_CLIENTOWNED; Binding.eParamIO = DBPARAMIO_NOTPARAM; Binding.cbMaxLen = 4000 + 1; Binding.wType = DBTYPE_STR; Binding.dwFlags = 0; Binding.bPrecision = 0; Binding.bScale = 0; CComPtr&lt;IAccessor&gt; spIAccessor; hr = spIRowset-&gt;QueryInterface(IID_IAccessor, (void**) &amp;spIAccessor); HACCESSOR hAccessor = NULL; hr = spIAccessor-&gt;CreateAccessor(DBACCESSOR_ROWDATA, 1, &amp;Binding, 0, &amp;hAccessor, NULL); hr = spIRowset-&gt;GetData(hRow, hAccessor, &amp;data); DBREFCOUNT cRefCount = 0; hr = spIAccessor-&gt;ReleaseAccessor(hAccessor, &amp;cRefCount); spIAccessor = NULL; // @@TODO: Do something with data.szArticle and data.nArticleLength // ... // Fetch next row of data. hr = spIRowset-&gt;ReleaseRows(1, rghRow, NULL, NULL, NULL); cRowsObtained = 0; hr = spIRowset-&gt;GetNextRows(DB_NULL_HCHAPTER, 0, 1, &amp;cRowsObtained, &amp;rghRow); } // Release everything spIRowset = NULL; spIDBInitialize = NULL; CoUninitialize(); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload