想在程序里动态的设定ado控件的数据源,在添加ado控件的时候什么属性都没有手工设置,在程序里用了语句:
  m_adoc.SetConnectionString("Provider=SQLOLEDB.1;Persist Security Info=False;");
 //"Provider=SQLOLEDB.1;User ID=qqq;Password=qqq;Initial Catalog=qqq;Data Source=qq;也不行  
  m_adoc.SetCommandType(1);
  m_adoc.SetUserName("gg");
  m_adoc.SetPassword("ggg");
  str="SELECT * FROM gg表 "; 
  m_adoc.SetRecordSource(str);
  m_adoc.Refresh();
  可是datagrid根本没有反应,(连表的各列也不显示,说明根本没有在查询)
   但是用
   str=m_adoc.GetConnectionString();
   str=m_adoc.GetRecordSource();
   看到ado控件的属性已经通过程序设置成功了   datagrid已经手动的绑定到这个adoc控件上了,手动设置adoc控件各属性显示没有问题。
 
   是不是还需要在程序里设置什么东西呢?
   请教各位了。  

解决方案 »

  1.   

    _CommandPtr cmd;  //command object
    _RecordsetPtr rs;    //recordset object
    _ConnectionPtr conn; //connection object
    _variant_t vra;
    VARIANT *vt1 = NULL;
    try
    {
    //create instance of Command, Connection and Recordset.
    cmd.CreateInstance( __uuidof(Command));
    rs.CreateInstance(__uuidof(Recordset));
    conn.CreateInstance(__uuidof(Connection));
    // used for RXF_text data exchange
    UpdateData(TRUE);

    conn->CursorLocation = adUseClient;
    //Get connection string from edit control and open connection
    conn->Open(_bstr_t( m_strConnect.GetBuffer(0) ), L"", L"", -1);

    //Assign oppened connection object to Command object
    cmd->ActiveConnection = conn;

    //Get query text from edit control
    cmd->CommandText = (_bstr_t) m_strQuery.GetBuffer(0);
    cmd->CommandType = adCmdText;

    //execute the command and assign returning Recordset to previously 
    //instanciated recordset object
    rs = cmd->Execute(&vra,vt1, adCmdText);
    } //Error catching
    catch (_com_error &e)
    {
    GenerateError(e.Error(), e.Description());
    }
         catch (...)
    {
            
    } //casting to LPUNKNOWN is important so it will query interface Recordset object 
    //for IUnknown
    m_myDataGrid.SetRefDataSource( (LPUNKNOWN) rs );
    m_myDataGrid.Refresh();
    UpdateData(FALSE);
      

  2.   

    我测试过了,好用的,
    如在你那不好用的话,请留下你的Email我给你发一个小demo