SQL Sever 2014 Query Notifications in C++ with ADO.NET libraries

232 Views Asked by At

I have a C++ application that has to react to status changes that are kept in an SQL database. They have to react in real-time, and currently this is done by polling. However the real-time response time I need is overloading the database.

I have read about Query Notifications introduced in SQL Server 2005, but in truth I don't really understand the different SQL connection methods available. Currently I use ADO.NET to faciliate database communication, but the only sample code I have found for doing Query Notifications of this nature is in C# and VB. Not to mention that I am struggling to understand the concept. Here is a sample of how I currently run SQL with ADO.NET.

    hr = link.CreateInstance(__uuidof(Connection) );

        if (link) { // Execute the query on the open connection using existing command object

        pCommand.CreateInstance(__uuidof(Command));
        pCommand->ActiveConnection = link;
        pCommand->CommandType = ado20CommandType;
        pCommand->CommandText = strQuery.GetBuffer(0);
        pCommand->PutPrepared(true);
        pCommand->NamedParameters = true;

            if (pRecordSet = pCommand->Execute(&vRecordsAffected,NULL,NULL)) {  // Execute the final query and set object-level recordset object
            lRecordsAffected = vRecordsAffected.lVal;

            _RecordsetPtr pfuRecordSet = link->Execute(_T("SELECT @@IDENTITY as pmIDENT;"),NULL,adCmdText);
            CString strID = pfuRecordSet->GetFields()->GetItem(COleVariant("pmIDENT"))->Value.bstrVal;
            int nID = atoi(strID);
            if (nID > 0) {
                nLastInsertedID = nID;
            }

            return true;
        }

I was wondering if someone could provide me with resources and/or sample code that would help me set this up in C++? Thanks in advance.

1

There are 1 best solutions below

3
On

The fastest way to get data out of a SQL Server database is using the SqlDataReader, which is forward-only, read-only.

Take a look at this C++ code and give that a try. http://tinyurl.com/m6u9jh7