Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 在VisualC++中执行查询最简单的方法是什么? 我使用Visual C++ 2005,想知道连接MS-SQL Server并执行查询的最简单方法。_Sql Server 2005_Visual C++_Mfc - Fatal编程技术网

Sql server 2005 在VisualC++中执行查询最简单的方法是什么? 我使用Visual C++ 2005,想知道连接MS-SQL Server并执行查询的最简单方法。

Sql server 2005 在VisualC++中执行查询最简单的方法是什么? 我使用Visual C++ 2005,想知道连接MS-SQL Server并执行查询的最简单方法。,sql-server-2005,visual-c++,mfc,Sql Server 2005,Visual C++,Mfc,我正在寻找像ADO.NET的SqlCommand类一样简单的东西,它有ExecuteOnQuery、ExecuteScalar和ExecuteReader Sigh使用CDatabase和ODBC提供了一个答案 有人能演示一下如何使用OleDb的ATL消费者模板来实现这一点吗 还可以从查询返回标量值吗?试试Microsoft企业库。C++应该有一个版本。SQlHelper类实现了您在旧ADO时代寻找的方法。如果您可以使用版本2,您甚至可以使用相同的语法。对于MFC,如果通过ODBC连接,请使用C

我正在寻找像ADO.NET的SqlCommand类一样简单的东西,它有ExecuteOnQuery、ExecuteScalar和ExecuteReader

Sigh使用CDatabase和ODBC提供了一个答案

有人能演示一下如何使用OleDb的ATL消费者模板来实现这一点吗


还可以从查询返回标量值吗?

试试Microsoft企业库。C++应该有一个版本。SQlHelper类实现了您在旧ADO时代寻找的方法。如果您可以使用版本2,您甚至可以使用相同的语法。

对于MFC,如果通过ODBC连接,请使用CDATA Base和ExecuteSQL

CDatabase db(ODBCConnectionString);
db.Open();
db.ExecuteSQL(blah);
db.Close();
我最近用过这个:

#include <ole2.h>
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include <oledb.h> 
void CMyDlg::OnBnClickedButton1()
{
    if ( FAILED(::CoInitialize(NULL)) )
        return;
    _RecordsetPtr pRs = NULL;

    //use your connection string here
    _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"));
    _bstr_t a_Select(_T("select * from Table"));


    try {

            pRs.CreateInstance(__uuidof(Recordset));
            pRs->Open(a_Select.AllocSysString(), strCnn.AllocSysString(), adOpenStatic, adLockReadOnly, adCmdText);

            //obtain entire restult as comma separated text:
            CString text((LPCWSTR)pRs->GetString(adClipString, -1, _T(","), _T(""), _T("NULL")));

            //iterate thru recordset:
            long count = pRs->GetRecordCount();

            COleVariant var;
            CString strColumn1;
            CString column1(_T("column1_name")); 

            for(int i = 1; i <= count; i++)
            {
                var = pRs->GetFields()->GetItem(column1.AllocSysString())->GetValue();
                strColumn1 = (LPCTSTR)_bstr_t(var);
            }
    }
    catch(_com_error& e) {
            CString err((LPCTSTR)(e.Description()));
            MessageBox(err, _T("error"), MB_OK);
            _asm nop; //
    }
   // Clean up objects before exit.
   if (pRs)
       if (pRs->State == adStateOpen)
              pRs->Close();



     ::CoUninitialize();
}
你应该能够使用这个。差不多是:

#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000
#include <otlv4.h> // include the OTL 4.0 header file
#include <stdio>

int main()
{
  otl_connect db; // connect object
  otl_connect::otl_initialize(); // initialize ODBC environment
  try
  {
    int myint;

    db.rlogon("scott/tiger@mssql2008"); // connect to the database
    otl_stream select(10, "select someint from test_tab", db);

    while (!select.eof())
    {
      select >> myint;
      std::cout<<"myint = " << myint << std::endl;
    }
  }
  catch(otl_exception& p)
  {
    std::cerr << p.code << std::endl;     // print out error code
    std::cerr << p.sqlstate << std::endl; // print out error SQLSTATE
    std::cerr << p.msg << std::endl;      // print out error message
    std::cerr << p.stm_text << std::endl; // print out SQL that caused the error
    std::cerr << p.var_info << std::endl; // print out the variable that caused the error
  }

  db.logoff(); // disconnect from the database

  return 0;
}
关于OTL,IMO的好处是它非常快速,可移植。我在许多平台上使用过它,并连接到许多不同的数据库