<p>I'm using Visual C++ 2005 and would like to know the simplest way to connect to a MS SQL Server and execute a query.</p>

<p>I'm looking for something as simple as ADO.NET's SqlCommand class with it's ExecuteNonQuery(), ExecuteScalar() and ExecuteReader().</p>

<p>Sigh offered an answer using CDatabase and ODBC.</p>

<p>Can anybody demonstrate how it would be done using ATL consumer templates for OleDb?</p>

<p>Also what about returning a scalar value from the query?</p>

[{'answer_id': 25542, 'author': 'Rob Allen', 'author_id': 149, 'author_profile': 'https://Stackoverflow.com/users/149', 'pm_score': 0, 'selected': False, 'text': '<p>Try the Microsoft Enterprise Library. A version should be available <a href="http://www.codeplex.com/entlib" rel="nofollow noreferrer">here</a> for C++. The SQlHelper class impliments the methods you are looking for from the old ADO days. If you can get your hands on version 2 you can even use the same syntax.</p>\n'}, {'answer_id': 25544, 'author': 'Sigh', 'author_id': 1866, 'author_profile': 'https://Stackoverflow.com/users/1866', 'pm_score': 2, 'selected': False, 'text': '<p>With MFC use CDatabase and ExecuteSQL if going via a ODBC connection.</p>\n\n<pre><code>CDatabase db(ODBCConnectionString);\ndb.Open();\ndb.ExecuteSQL(blah);\ndb.Close();\n</code></pre>\n'}, {'answer_id': 154431, 'author': 'rec', 'author_id': 14022, 'author_profile': 'https://Stackoverflow.com/users/14022', 'pm_score': 1, 'selected': False, 'text': '<p>I used this recently:</p>\n\n<pre><code>#include &lt;ole2.h&gt;\n#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")\n#include &lt;oledb.h&gt; \nvoid CMyDlg::OnBnClickedButton1()\n{\n    if ( FAILED(::CoInitialize(NULL)) )\n        return;\n    _RecordsetPtr pRs = NULL;\n\n    //use your connection string here\n    _bstr_t strCnn(_T("Provider=SQLNCLI;Server=.\\\\SQLExpress;AttachDBFilename=C:\\\\Program Files\\\\Microsoft SQL Server\\\\MSSQL.1\\\\MSSQL\\\\Data\\\\db\\\\db.mdf;Database=mydb;Trusted_Connection=Yes;MARS Connection=true"));\n    _bstr_t a_Select(_T("select * from Table"));\n\n\n    try {\n\n            pRs.CreateInstance(__uuidof(Recordset));\n            pRs-&gt;Open(a_Select.AllocSysString(), strCnn.AllocSysString(), adOpenStatic, adLockReadOnly, adCmdText);\n\n            //obtain entire restult as comma separated text:\n            CString text((LPCWSTR)pRs-&gt;GetString(adClipString, -1, _T(","), _T(""), _T("NULL")));\n\n            //iterate thru recordset:\n            long count = pRs-&gt;GetRecordCount();\n\n            COleVariant var;\n            CString strColumn1;\n            CString column1(_T("column1_name")); \n\n            for(int i = 1; i &lt;= count; i++)\n            {\n                var = pRs-&gt;GetFields()-&gt;GetItem(column1.AllocSysString())-&gt;GetValue();\n                strColumn1 = (LPCTSTR)_bstr_t(var);\n            }\n    }\n    catch(_com_error&amp; e) {\n            CString err((LPCTSTR)(e.Description()));\n            MessageBox(err, _T("error"), MB_OK);\n            _asm nop; //\n    }\n   // Clean up objects before exit.\n   if (pRs)\n       if (pRs-&gt;State == adStateOpen)\n              pRs-&gt;Close();\n\n\n\n     ::CoUninitialize();\n}\n</code></pre>\n'}, {'answer_id': 154495, 'author': 'Zathrus', 'author_id': 16220, 'author_profile': 'https://Stackoverflow.com/users/16220', 'pm_score': 2, 'selected': False, 'text': '<p>You should be able to use <a href="http://otl.sourceforge.net/home.htm" rel="nofollow noreferrer">OTL</a> for this. It\'s pretty much:</p>\n\n<pre><code>#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008\n//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000\n#include &lt;otlv4.h&gt; // include the OTL 4.0 header file\n#include &lt;stdio&gt;\n\nint main()\n{\n  otl_connect db; // connect object\n  otl_connect::otl_initialize(); // initialize ODBC environment\n  try\n  {\n    int myint;\n\n    db.rlogon("scott/tiger@mssql2008"); // connect to the database\n    otl_stream select(10, "select someint from test_tab", db);\n\n    while (!select.eof())\n    {\n      select &gt;&gt; myint;\n      std::cout&lt;&lt;"myint = " &lt;&lt; myint &lt;&lt; std::endl;\n    }\n  }\n  catch(otl_exception&amp; p)\n  {\n    std::cerr &lt;&lt; p.code &lt;&lt; std::endl;     // print out error code\n    std::cerr &lt;&lt; p.sqlstate &lt;&lt; std::endl; // print out error SQLSTATE\n    std::cerr &lt;&lt; p.msg &lt;&lt; std::endl;      // print out error message\n    std::cerr &lt;&lt; p.stm_text &lt;&lt; std::endl; // print out SQL that caused the error\n    std::cerr &lt;&lt; p.var_info &lt;&lt; std::endl; // print out the variable that caused the error\n  }\n\n  db.logoff(); // disconnect from the database\n\n  return 0;\n}\n</code></pre>\n\n<p>The nice thing about OTL, IMO, is that it\'s very fast, portable (I\'ve used it on numerous platforms), and connects to a great many different databases.</p>\n'}]
['https://Stackoverflow.com/questions/25499', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2768/']