就像Oracle 和 MS SQL Server中自带的运行SQL语句,打印运行结果的功能一样
请问有甚么好的方案吗?
有贴点代码更好
不胜感激

解决方案 »

  1.   

    如果你用ADO的话,你就列举_RecordsetPtr指针上的记录集,跟据记录集生成表结构。
    获得列数,获取行数,用列表控件相应的产生列和行,把数据显示上去就行了。
      

  2.   

    谢谢, 我也想过
    不过除了需要查询表,还有insert,update,delete,CreateTable, CreateView等操作,怎样写最好呢? 最好能够考虑通用性
    程序越小越好,因为要放在远程的肉鸡上运行的, 用 winsocket发送 SQL语句来控制的, 对方是MySQL数据库, WindowXP
    不胜感激
      

  3.   

    BOOL CDlgDBConnect::ConnectOracle()
    {
    if (m_strService.IsEmpty())
    {
    ::MessageBox(NULL,"·þÎñÃû²»ÄÜΪ¿Õ","Ìáʾ",MB_OK);
    return FALSE;
    } theApp.m_pConnection.CreateInstance(__uuidof(Connection)); 
    try                 
    {
    //OLEDBÇý¶¯
    theApp.m_strDatabase=m_strService;
    CString strConn;
    strConn="Provider=OraOLEDB.Oracle.1;Password=";
    strConn+=m_strPassword;
    strConn+=";Persist Security Info=True;User ID=";
    strConn+=m_strUsername;
    // strConn+=";SERVER=";
    // strConn+=m_strService;
    strConn+=";Data Source=";
    strConn+= m_strService;
    theApp.m_pConnection->Open((_bstr_t)strConn,
       "",
       "",
       adModeUnknown);
    }
    catch(_com_error e)
    {
    ::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
    return FALSE;
    }

    ::MessageBox(NULL,"Êý¾Ý¿âÁ¬½Ó³É¹¦£¡","Ìáʾ",MB_OK);
    return TRUE;}BOOL CDlgDBConnect::ConnectSQLServer()
    {
    if (m_strService.IsEmpty())
    {
    ::MessageBox(NULL,"·þÎñÃû²»ÄÜΪ¿Õ","Ìáʾ",MB_OK);
    return FALSE;
    }
    if (m_strDatabase.IsEmpty())
    {
    ::MessageBox(NULL,"Êý¾Ý¿â²»ÄÜΪ¿Õ","Ìáʾ",MB_OK);
    return FALSE;
    } theApp.m_pConnection.CreateInstance(__uuidof(Connection)); 
    try                 
    {
    theApp.m_strDatabase=m_strDatabase; CString strConn;
    strConn="Provider=SQLOLEDB.1;Password=";
    strConn+=m_strPassword;
    strConn+=";Persist Security Info=True;User ID=";
    strConn+=m_strUsername;
    strConn+=";Initial Catalog=";
    strConn+=m_strDatabase;
    strConn+=";Data Source=";
    strConn+=m_strService;
    theApp.m_pConnection->Open((_bstr_t)strConn,
       "",
       "",
       adModeUnknown);
    }
    catch(_com_error e)
    {
    ::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
    return FALSE;
    }

    ::MessageBox(NULL,"Êý¾Ý¿âÁ¬½Ó³É¹¦£¡","Ìáʾ",MB_OK);
    return TRUE;}
    BOOL CDlgRecordset::QueryRecord()
    {
    _CommandPtr cmd;  
    _RecordsetPtr rs;  
    _ConnectionPtr conn; 
    _variant_t vra;
    VARIANT *vt1 = NULL;
    try
    {
    /* cmd.CreateInstance( __uuidof(Command));
    rs.CreateInstance(__uuidof(Recordset));
    conn.CreateInstance(__uuidof(Connection));*/ UpdateData(TRUE);
    theApp.m_pConnection->PutCursorLocation(adUseClient);

    /* conn->CursorLocation = adUseClient;

    conn->Open(_bstr_t( theApp.m_pConnection.GetInterfacePtr() ), L"", L"", -1);
    cmd->ActiveConnection = conn;

       cmd->CommandText = (_bstr_t) m_strQuery;
    cmd->CommandType = adCmdText;
    rs = cmd->Execute(&vra,vt1, adCmdText);*/
    /*rs->PutRefActiveConnection(theApp.m_pConnection);
    rs->Open((_bstr_t)m_strQuery,
    theApp.m_pConnection.GetInterfacePtr(),
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);*/
    ///     ((CTestDllApp*)(&theApp))->m_pConnection
    /// m_pRecordset.CreateInstance(__uuidof(Recordset));

    /* m_pRecordset->Open((_bstr_t)m_strQuery,
    _variant_t((IDispatch*)theApp.m_pConnection, TRUE),
    adOpenStatic,
    adLockOptimistic,
    adCmdText);*/
       if(m_pRecordset->State==1)
    {
    m_pRecordset->Close();
    }
    m_pRecordset->Open((_bstr_t)m_strQuery,
    theApp.m_pConnection.GetInterfacePtr(),//»ñÈ¡¿â½Ó¿âµÄIDispatchÖ¸Õë
    adOpenStatic,
    adLockOptimistic,
    adCmdText);
    /* m_DataGrid.SetRefDataSource(NULL);
    m_DataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
    m_DataGrid.Refresh();*/
    }
    catch (_com_error &e)
    {
    ::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
    }

      

  4.   

    我目前的方法是判断sSQL语句的类型,分开写的if(是否为Select语句)
    {
    //用_RecordsetPtr
    }
    else
    {
    //用_CommandPtr
    }
    请问有没有一种通用的方法呢?
    不胜感激
      

  5.   

    用类封装吧,虚拟类sql,select类,update类....