Using ADO for SQL Server data access 
Author Message
 Using ADO for SQL Server data access

Hi folks,

Has anyone already used ADO to access SQL Server tables? If so, is it
possible to show me some sample code (mainly how to create the different ADO
objects).

Thanks,
Andrew



Mon, 06 Nov 2000 03:00:00 GMT  
 Using ADO for SQL Server data access

STDMETHODIMP CSWSConfTool::IsCategoryValid( long nDoc_category,
VARIANT_BOOL * varB )
// *********************
// Inputs:      nDoc_category is the page ID or document category to check for
validity.
// Outputs:     varB is set to false if the nDoc_category document category
//                              is not present in a record of the table top_page_table.
// *********************
{
        HRESULT hr = S_OK;

        ado15::_ConnectionPtr   adoConnection;
        ado15::_CommandPtr              adoCommand;
        ado15::_ParameterPtr    adoParameter;
        ado15::ParametersPtr    adoParameters;
        ado15::_RecordsetPtr    adoRecordset;

        adoConnection = NULL;
        adoCommand = NULL;
        adoRecordset = NULL;
        LPBYTE lpB = new BYTE[BUFFER_SIZE];
        try
        {
                short nRet;

                // Instantiate connection and recordset objects.
                ATLTRACE(_T("Datasource: %s User id: %s password: %s\n"),
(LPTSTR)m_strDatasource, (LPTSTR)m_strDBUserID, (LPTSTR)m_strDBPassword);
                if (
                        FAILED(hr = adoConnection.CreateInstance( __uuidof(ado15::Connection) ))
                        || FAILED(hr = adoCommand.CreateInstance( __uuidof(ado15::Command) ))
                        || FAILED(hr = adoRecordset.CreateInstance( __uuidof(ado15::Recordset)
))
                        || FAILED(hr = adoConnection->Open( m_strDatasource, m_strDBUserID,
m_strDBPassword, -1 ))
                ) {
                        goto ErrorExit;
                }
                // Open a recordset on our new table.
                _variant_t varCategory( (long)nDoc_category, VT_I4 );
                nRet = QueryRegistryKey( _T("FIND_D_C"), lpB, BUFFER_SIZE );
                if ( _IVSSWS_SUCCESS != nRet )
                        goto ErrorExit;
                adoCommand->PutCommandText( (LPTSTR)lpB );
                delete [] lpB;
                lpB = NULL;
                adoCommand->PutRefActiveConnection( adoConnection );
                adoParameter = adoCommand->CreateParameter(
                                _bstr_t(_T("Param2"))
                                , ado15::adInteger
                                , ado15::adParamInput
                                , -1
                                , varCategory
                );
                if ( FAILED(adoCommand->get_Parameters(&adoParameters)))
                        goto ErrorExit;
                // add the new CParameters into the ADO->Command->Parameters collection
                if ( FAILED( adoParameters->Append( adoParameter)) )
                        goto ErrorExit;
                VARIANT varValue = vtMissing;
                adoRecordset = adoCommand->Execute( &varValue, &varValue, -1 );
                if ( ! short(adoRecordset->Fields->Item[0L]->Value) )
                        goto ErrorExit;
        } catch (_com_error & err) {
                if ( lpB ) {
                        delete [] lpB;
                        lpB = NULL;
                }
                ATLTRACE(_T("%ld: %s\n"), err.Error(), err.ErrorMessage());
                goto ErrorExit;
        } catch (...) {
                if ( lpB ) {
                        delete [] lpB;
                        lpB = NULL;
                }
                goto ErrorExit;
        }
        *varB = VARIANT_TRUE;
        goto final_exit;
ErrorExit:
        *varB = VARIANT_FALSE;
final_exit:
        if ( adoRecordset ) {
                adoRecordset->Close();
                adoRecordset = NULL;
        }
        adoCommand = NULL;
        if ( adoConnection ) {
                adoConnection->Close();
                adoConnection = NULL;
        }
        return hr;

Quote:
}



Mon, 06 Nov 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Using ADO for SQL Server data access

2. Using ADO for SQL Server data access

3. Using ADO for SQL Server data access

4. Accessing MS SQL-Server via ADO

5. ADO to access an SQL Server 7.0 database

6. Database access with ado , rds2.1 and sql server 7

7. Using OLE DB or ADO to connect Oracle and SQL Server

8. Storing date/time value on SQL server with milliseconds resolution using vc++ and ado 2.0

9. Access SQL Server 2000 Data Analysis Services

10. ACCESS vs SQL Server: binary data

11. Data Access With C# and SQL Server

12. Help needed: problem in using odbc call to write binary data to SQL Server

 

 
Powered by phpBB® Forum Software