//ODBC open SQL server database
CDatabase db;
db.OpenEx("DRIVER={SQL Server};Server=Cell;DATABASE=zhg;UID=sa;PWD=;");//change Cell to your computer name
//change zhg to your database name
CRecordset rs(&db);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select type,AVG(price)'average price' From title  where type<>'business' group by type");
while(rs.IsEOF()==false)
{
CString str;
CString strtype;
rs.GetFieldValue((short)0,strtype);
CDBVariant varprice;
rs.GetFieldValue(1,varprice);
str.Format("type:%s,avg(price):%f",strtype,varprice.m_dblVal);
AfxMessageBox(str);
rs.MoveNext();
}
rs.Close();
db.Close();

解决方案 »

  1.   

    2.console application
    #include "stdafx.h"
    #import "H:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
    int main(int argc, char* argv[])
    {
       CoInitialize(NULL);
       try
       {
          _ConnectionPtr pConn("ADODB.Connection");
          _RecordsetPtr  pRst("ADODB.Recordset");      pConn->Open("Provider=sqloledb;Data Source=Server1;"
             "Initial Catalog=test1;User Id=sa;Password=sa;",
             "", "", adConnectUnspecified);
    // Note 1.
          pRst->Open(
             "userinfo",
             _variant_t((IDispatch *) pConn, true),
             adOpenStatic,
             adLockReadOnly,
             adCmdTable);
          pRst->MoveLast();
    // Note 2.
          printf("Last name is '%s %s'\n",
                (char*) ((_bstr_t) pRst->GetFields()->GetItem("username")->GetValue()),
                (char*) ((_bstr_t) pRst->Fields->Item["otherinfo"]->Value));      pRst->Close();
          pConn->Close();
       }
       catch (_com_error &e)
       {
          printf("Description = '%s'\n", (char*) e.Description());
       }
    ::CoUninitialize();return 0;
    }
      

  2.   

    to masterz:
    为什么下面这段能工作:
    #include <afxwin.h>
    #import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
    int main(int argc, char* argv[])
    {
      CoInitialize(NULL);
      try
      {
          _ConnectionPtr pConn("ADODB.Connection");
          _RecordsetPtr  pRst("ADODB.Recordset");
          pConn->Open("Provider=sqloledb;Server=jwglsql;database=ssdjwgl;UId=sa;Password=0712;",
            "sa", "0712", adConnectUnspecified);
          pRst->Open(
            "部门专业对应表",
            _variant_t((IDispatch *) pConn, true),
            adOpenStatic,
            adLockReadOnly,
            adCmdTable);
            pRst->MoveLast();
          //while(!pRst->EndOfFile){
      printf("%s,%s,%s,%s,%s,%s",(char *)((_bstr_t) pRst->GetFields()->GetItem("部门代码")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("部门名称")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("校内专业代码")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("校内专业名称")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("专业代码")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("专业名称")->GetValue()));
     //pRst->MoveNext();
      //}      pRst->Close();
          pConn->Close();
      }
      catch (_com_error &e)
      {
          printf("Description = '%s'\n", (char*) e.Description());
      }
    ::CoUninitialize();return 0;

    而这段代码却不能:
    #include <afxwin.h>
    #include <iostream.h>
    #import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
    int main(int argc, char* argv[])
    {
      CoInitialize(NULL);
      try
      {
          _ConnectionPtr pConn("ADODB.Connection");
          _RecordsetPtr  pRst("ADODB.Recordset");
          pConn->Open("Provider=sqloledb;Server=jwglsql;database=ssdjwgl;UId=sa;Password=0712;",
            "sa", "0712", adConnectUnspecified);
          pRst->Open(
            "部门专业对应表",
            _variant_t((IDispatch *) pConn, true),
            adOpenStatic,
            adLockReadOnly,
            adCmdTable);
            pRst->MoveFirst();
            while(!pRst->EndOfFile){
      printf("%s,%s,%s,%s,%s,%s",(char *)((_bstr_t) pRst->GetFields()->GetItem("部门代码")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("部门名称")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("校内专业代码")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("校内专业名称")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("专业代码")->GetValue()),
      (char *)((_bstr_t) pRst->GetFields()->GetItem("专业名称")->GetValue()));
     pRst->MoveNext();
      }      pRst->Close();
          pConn->Close();
      }
      catch (_com_error &e)
      {
          printf("Description = '%s'\n", (char*) e.Description());
      }
    ::CoUninitialize();return 0;
      

  3.   

    to masterz:
    用odbc的例子怎么老出现:assertion failure
      

  4.   

    I don't know.
    It works well on my computer.And Do you know how to debug your program? Why not run it step by step to find out the problem?