我在学做一个ADO数据库编程,就是在对话框根据一个编辑框中的语句对数据库进行查询或插入,然后在一个DataGrid控件中显示结果,为了进行多次查询,是否必须每次操作m_pRecordset->Open....都需要关闭m_pRecordset,如果不关闭会有什么问题吗?(我的程序中只是在对话框退出时关闭记录集与数据库连接。

解决方案 »

  1.   

    对于同一个recordset实例。open之后,就不允许再次调用open方法,所以在open之前要检查state是否是关闭的。如果没有关闭,必须先关闭。再OPEN
    之所以在更新数据和添加数据之后,open,因为使用insert ,update,delete,SQL语句来操作记录,会直接影响数据库源的实际数据。而此时open的recordset实际上保存的是缓存到client的记录记,为了看到更改过的数据,需要从新open从数据源中得到新数据
    当然可设置recordset,为服务端游标,不过效率和数据连接稳定性嘛就有点不好保证了
      

  2.   

    m_pRecordset = m_pConnection->Execute("SELECT COUNT(*) FROM users",&RecordsAffected,adCmdText);   这样做更方便一些,m_pRecordset的实例都不用创建。
      

  3.   

    谢谢楼上各位提供的好建议!!!我想问一下:
    “m_pRecordset = m_pConnection->Execute("SELECT COUNT(*) FROM uers",&RecordsAffected,adCmdText);”
    我看过这方面介绍,网上也下载过示例,可能对我来讲复杂了一些,你能否给我一个较简单的示例吗?比如:只建一个对话框,上面有三个编辑控件,一个用于填入“查询”或“插入”语句,二个用于显示查询结果,二个按钮,一个用于查询,一个用于插入新的记录。
    另外每次操作是否也要Close一下
    不好意思,水平太差了,请多多指教,谢谢!!!
      

  4.   

    recordset.open方法可以运行大多数的sql查询语句select,delete,update,insert into
    其中只有select会返回记录集,必须要close,其他的操作不返回记录集的可以不用close
      

  5.   

    我下午将我的问题写成一个小程序,麻烦给我看一下,折腾了几天,真郁闷
    从程序上看,查询已成功了,但是程序中在填入DataGrid控件时出现这问题The rowset is not bookable.
    从网上查应该用“m_pConnection.CursorLocation=adUseClient;”解决,但记录集没有这个属,不知是我没看懂还是专家给搞错了。
    怎么没办法传文件,只好写这上面了:一个CDataGrid(控件变量是m_ctrl)、一个CEdit(字符串变量m_stredit)、一个查询按钮;然后有三个_CommandPtr m_commandptr;
    _RecordsetPtr m_pUserSet;_ConnectionPtr m_pConnection;
    int CCccDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;
    // TODO: Add your specialized creation code here
    CString strSQL;
    AfxOleInit();
    m_pConnection.CreateInstance("ADODB.Connection");
    // m_pConnection.CursorLocation=adUseClient;
    strSQL="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";
    try                 
    {
    //打开mdb库perConnection.mdb
    m_pConnection->Open((_bstr_t)strSQL,"","",-1);
    }
    catch (_com_error e)
    {
    CString strError;
    strError.Format("警告:打开连接发生异常。 错误信息: %s",e.ErrorMessage());
    AfxMessageBox(strError);
    return FALSE;
    }
    try
    {
    m_commandptr.CreateInstance (__uuidof(Command));
    m_pUserSet.CreateInstance (__uuidof(Recordset));
    m_commandptr->ActiveConnection =m_pConnection;
    }
    catch(_com_error *e)
    {
    AfxMessageBox(e->ErrorMessage());
    } return 0;
    }
    void CCccDlg::OnClose() 
    {
    // TODO: Add your message handler code here and/or call default
    if(m_commandptr != NULL)
    {
    if (m_commandptr->State )
    {
    m_commandptr = NULL;
    }
    }

    if(m_pUserSet != NULL)
    {
    if (m_pUserSet->State)
    {
    m_pUserSet->Close ();
    m_pUserSet = NULL;
    }
    }
    if (m_pConnection != NULL)
    {
    if (m_pConnection->State)
    {
    m_pConnection->Close ();
    }
    m_pConnection = NULL;
    }
    CDialog::OnClose();
    }void CCccDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    _variant_t fieldCount;
    VariantInit (&fieldCount); UpdateData(true);
    m_commandptr->CommandType =adCmdText;
    m_commandptr ->CommandText =(_bstr_t)m_stredit;
    m_pUserSet = m_commandptr->Execute (&fieldCount,NULL,adCmdUnknown);
    AfxMessageBox("OK");
    m_ctrl.SetRefDataSource(NULL);
    m_ctrl.SetRefDataSource((LPUNKNOWN)m_pUserSet);
    }
      

  6.   

    CDATAGrid控件哪儿来的,给我用用好吗?
      

  7.   

    m_pUserSet->cursorLocaltion = adUseClient;