1

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.

Raiden616
  • 1,545
  • 3
  • 18
  • 42
  • Questions requesting resources on the web are off-topic on SO. You need to ask about some specific problem that you have. A potential solution would be to create a C# project and call it from C++. – usr May 23 '15 at 11:30

1 Answers1

0

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

Carl Prothman
  • 1,461
  • 13
  • 23
  • Awesome thank you, yeah see this is where I'm a little unclear. So I would have to implement sqldatareader alongside by ado.net code for when I want to do write queries I assume... That's pretty simple. But can sqldatareader support query notifications or do is this just an alternative method to reading with ado.net that is faster? My research would suggest the former.. – Raiden616 May 24 '15 at 12:00
  • Yes, you can use SqlDataReader with SqlDependency (C# solution) https://msdn.microsoft.com/en-us/library/9dz445ks(v=vs.110).aspx – Carl Prothman May 24 '15 at 13:58
  • AddCacheDependency (C# solution) https://msdn.microsoft.com/en-us/library/9dz445ks(v=vs.110).aspx – Carl Prothman May 24 '15 at 14:00