如何得到一个已经存在表的字段数目啊?我是VC+ADO +Access编程,有没有SQL语句可以用啊??

解决方案 »

  1.   

    这个例子很经典阿http://www.csdn.net/cnshare/soft/14/14373.shtm
      

  2.   

    _RecordsetPtr  m_pRs;  
    FieldsPtr  m_pFields;  
    FieldPtr  m_pField;  
    m_pFields=m_pRs->GetFields();  
    int  index=m_pFields->Count;//index就是字段数
      

  3.   

    to yuanbocsut(打盹的神仙)  
    http://www.csdn.net/cnshare/soft/14/14373.shtm,这个例子看不明白,因为我不知道运行起来该怎么用 
     
      

  4.   

    to laiyiling(最熟悉的陌生人),我用的是ADO,你所说的指针定义就游问题啊,
    FieldsPtr  m_pFields; //没有该类型 
    FieldPtr  m_pField;//没有该类型  
    而 m_pRs也没有GetFields()的成员函数  
     
      

  5.   


    laiyiling(最熟悉的陌生人):ok!
      

  6.   

    // 加入ADO支持库
    #import "c:\program files\common files\system\ado\msado15.dll"  no_namespace  rename ("EOF", "adoEOF")  
    记得 CoInitialize(NULL)
    和 UnCoInitialize()
      

  7.   

    楼上各位,不行啊
    我用ADO也不是一天两天了。。只是一直没用过
    FieldsPtr  m_pFields;  
    FieldPtr  m_pField;  
      

  8.   

    全复制过来了,改连接字符串和查询语句一下就可以,我已经测试过了。// syb1Dlg.cpp : implementation file
    //#include "stdafx.h"
    #include "syb1.h"
    #include "syb1Dlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif#include <comdef.h>
    #import "C:\program files\common files\system\ado\msado26.tlb"  no_namespace  rename("EOF", "adoEOF")/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CSyb1Dlg dialogCSyb1Dlg::CSyb1Dlg(CWnd* pParent /*=NULL*/)
    : CDialog(CSyb1Dlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CSyb1Dlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); //com
    ::CoInitialize(NULL);
    }void CSyb1Dlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CSyb1Dlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CSyb1Dlg, CDialog)
    //{{AFX_MSG_MAP(CSyb1Dlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_WM_DESTROY()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CSyb1Dlg message handlersBOOL CSyb1Dlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CSyb1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CSyb1Dlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CSyb1Dlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CSyb1Dlg::OnButton1() 
    {
    //测试连接sybase数据库。
    _ConnectionPtr ptrConnect;
    _RecordsetPtr ptrRcdSet; ptrConnect.CreateInstance(_uuidof(Connection));
    CString szSQL;
    szSQL = "select * from tab_1";
    HRESULT hr = ptrConnect->Open("FILE NAME=C:\\Program Files\\Common Files\\ODBC\\Data Sources\\mysybase.dsn","sa","",0);
    if(FAILED(hr)) return;
    _variant_t vRecsAffected(0L); //ptrRcdSet = ptrConnect->Execute(_bstr_t(szSQL),&vRecsAffected,adOptionUnspecified);

    _variant_t varCode1;
    _variant_t varCode2;
    try
    {
    CString strMsg;
    ptrRcdSet.CreateInstance("ADODB.Recordset");
    ptrRcdSet->Open(_bstr_t(szSQL),"FILE NAME=C:\\Program Files\\Common Files\\ODBC\\Data Sources\\mysybase.dsn",adOpenStatic,adLockReadOnly,adCmdText);
    if(!ptrRcdSet->GetadoEOF())
    {
    ptrRcdSet->MoveFirst();
    while (!ptrRcdSet->GetadoEOF()) {
    FieldsPtr fields = ptrRcdSet->GetFields();
    FieldPtr field = fields->GetItem(_bstr_t("code1"));
    _variant_t variant1;
    field->get_Value(&variant1);
    varCode1 = ptrRcdSet->GetCollect("code1");
    varCode2 = ptrRcdSet->GetCollect("code2");

    //test
    strMsg.Format("code1 = %f,code2 = %f,fieldcount = %d",(double)varCode1,(double)varCode2,fields->GetCount());
    AfxMessageBox(strMsg);
    //保证提示fieldcount = 2
    ptrRcdSet->MoveNext();
    }
    }
    ptrRcdSet->Close();
    }
    catch(CException *pEx)
    {
    //MFC 异常
    char pszMsg[256];
    pEx->GetErrorMessage(pszMsg,256); TRACE("TestFunc,MFC ERROR:%s",pszMsg);
    }
    catch(_com_error &eX)
    {
    //Com 异常
    TRACE("TestFunc,COM ERROR:%s",eX.ErrorMessage());
    }
    catch(...)
    {
    //未知的异常
    TRACE("TestFunc,UNKNOWN ERROR");
    }
    ptrConnect->Close();
    }void CSyb1Dlg::OnDestroy() 
    {
    ::CoUninitialize();
    CDialog::OnDestroy();
    }
      

  9.   

    /*========================================================================
    Name: 获取当前记录集中字段数目
    ==========================================================================*/
    long CAdoRecordSet::GetFieldsCount()
    {
    ASSERT(m_pRecordset != NULL);
    try
    {
    return GetFields()->Count;
    }
    catch(_com_error e)
    {
    TRACE(_T(":( GetFieldsCount发生异常: %s\n"), e.ErrorMessage());
    return -1;

    }你可以执行select语句获取某一条记录的所有字段值,如select * from student where name = "kvw3000",利用上面的方法就可以获得这个表的字段数