使用ADO,Microsoft.Jet.OLEDB.4.0,不支持ALTER TABLE ... ADD ... 语句
该如何添加?

解决方案 »

  1.   


    我以前用 ALTER TABLE 改过;
    不知道楼主为什么说不支持?
      

  2.   


    CoInitialize(NULL); _ConnectionPtr m_pConnection; m_pConnection.CreateInstance(__uuidof(Connection));
    CString strOpenParam = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
    strOpenParam += "D:\\test.mdb"; try
    {
    m_pConnection->Open((_bstr_t)strOpenParam,"","",adModeUnknown);
    }
    catch(_com_error e)
    {
    AfxMessageBox("数据库连接失败,确认数据库data.mdb是否在当前路径下!");
    }//  _RecordsetPtr m_pRecordset;
    //  m_pRecordset.CreateInstance(__uuidof(Recordset));
    // 
    //  try
    //  {
    //  m_pRecordset->Open("SELECT * FROM OperatorTable", // OperatorTable
    //  /*theApp.*/m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
    //  adOpenStatic,
    //  adLockOptimistic,
    //  adCmdText
    //  );
    //  }
    //  catch(_com_error *e)
    //  {
    //  AfxMessageBox(e->ErrorMessage());
    //  }
      m_pConnection->Execute("ALTER TABLE OperatorTable ADD Detail double", &RecordsAffected, adCmdText);
    //  m_pRecordset->Close();
    //  m_pRecordset = NULL; if(m_pConnection->State)
            m_pConnection->Close();
    m_pConnection = NULL; CoUninitialize();
      

  3.   

    上面是我的代码,(中间少了条定义 _variant_t RecordsAffected;)
    运行时出现Runtime Error!
    用m_pRecordset打开表后再添加也是一样
      

  4.   

    ALTER TABLE OperatorTable ADD COLUMN Detail double
      

  5.   

    增加表列的时候,不要打开表,可能会出错。
    1、连接数据库;2、发送SQL语句增加列。
    下面是我用过的代码:m_pConnection->Execute((_bstr_t)strSQL,NULL,adCmdText);
    另外,你这句SQL先手工拿到ACCESS中去测试一下,看能不能增加列:ALTER TABLE OperatorTable ADD Detail double
      

  6.   

    Sorry,应是_variant_t RecordsAffected;
    m_pConnection->Execute("ALTER TABLE OperatorTable ADD COLUMN Detail double", &RecordsAffected, adCmdText);仍然Runtime Error
      

  7.   

    void CUpdateDBDlg::OnUpdateDB()
    {
    // TODO: 在此添加控件通知处理程序代码
    //获取所有数据库表名
    ::CoInitialize(NULL); CString strSql;
    CString strConnection;
    CString strMsg;
    CString strTemp, strAddField; _variant_t RecordsAffected;
    _ConnectionPtr pConnection;
    _RecordsetPtr pRecordset;
    GetDlgItemText( IDC_EDIT_UPDATEDB, m_strSelectDB);
    if( m_strSelectDB.IsEmpty() || TEXT("XXXXX.mdb") != m_strDBName )
    {
    ::MessageBox(NULL,_T("请选择名为XXXXX.mdb的数据库。"), _T("提示"),MB_OK | MB_ICONINFORMATION);
    }
    else
    {
    CUpdateDBDlg::GetTableName();
    strConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;"),m_strSelectDB);
    //strConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;"),TEXT("D:\\Download\\criminal.mdb"));
    HRESULT hr = pConnection.CreateInstance( __uuidof(Connection) );
    if ( FAILED(hr) )
    {
    ::MessageBox(NULL,_T("您系统中的数据库驱动有问题。"), _T("提示"),MB_OK | MB_ICONINFORMATION);
    }
    pRecordset.CreateInstance( __uuidof(Recordset));
    try
    {
    pConnection->ConnectionTimeout = 18;
    pConnection->ConnectionString =_bstr_t(strConnection);
    pConnection->Open( _bstr_t(L""), _bstr_t(L""), _bstr_t(L""), -1 );
    for(std::list<CString>::iterator strNewCoverField = listNewCoverFieldName.begin(); strNewCoverField != listNewCoverFieldName.end(); strNewCoverField++)
    {
    strTemp = *strNewCoverField;
    if( _T("CaseYear") == strTemp || _T("CaseNumber") == strTemp || _T("Deal") == strTemp || _T("TotalPages") == strTemp || _T("TotalPhotos") == strTemp || _T("TotalDeal") == strTemp )
    {
    strAddField = TEXT("ALTER TABLE Covers ADD COLUMN ") + strTemp + TEXT(" int");
    pConnection->Execute(_bstr_t(strAddField),&RecordsAffected,adCmdText);
    }
    if( _T("DoneDate") == strTemp || _T("PhotoDate") == strTemp || _T("MakeDate") == strTemp)
    {
    strAddField = TEXT("ALTER TABLE Covers ADD COLUMN ") + strTemp + TEXT(" datetime");
    pConnection->Execute(_bstr_t(strAddField),&RecordsAffected,adCmdText);
    }
    if( _T("CaseSubject") == strTemp || _T("Department") == strTemp || _T("CasePlace") == strTemp || _T("CaseName") == strTemp || _T("CaseKind") == strTemp || _T("WhoPhoto") == strTemp || _T("MakeUnit") == strTemp || _T("Makers") == strTemp || _T("Assessor") == strTemp)
    {
    strAddField = TEXT("ALTER TABLE Covers ADD COLUMN ") + strTemp + TEXT(" varchar(50)");
    pConnection->Execute(_bstr_t(strAddField),&RecordsAffected,adCmdText);
    }
    if( _T("CaseAbstract") == strTemp )
    {
    strAddField = TEXT("ALTER TABLE Covers ADD COLUMN ") + strTemp + TEXT(" text");
    pConnection->Execute(_bstr_t(strAddField),&RecordsAffected,adCmdText);
    }
    }
    ::MessageBox(NULL,_T("数据库更新成功。"), _T("提示"),MB_OK | MB_ICONINFORMATION);
    }
    catch( _com_error& e)
    {
    _bstr_t bstrError = e.Description();
    strMsg=(LPCTSTR)bstrError;
    ::MessageBox(NULL,strMsg, _T("提示"),MB_OK | MB_ICONINFORMATION);
    return;
    }
    pConnection->Close();
    pConnection.Release();
    }
    CoUninitialize();
    }
    这是我实际中的代码?
    有部分删减。。
    但是根本的东西还在
    你自己琢磨一下应该能解决
    关键是连接的方式不能和以前的操作一样
    pConnection->ConnectionTimeout = 18;
    pConnection->ConnectionString =_bstr_t(strConnection);
    pConnection->Open( _bstr_t(L""), _bstr_t(L""), _bstr_t(L""), -1 );
    注意这几行是关键的。。
      

  8.   

    !!
    不管是否runtimeerror,看看列是否已经添加进去了
    或许是别处出错
      

  9.   

    弄好了,谢谢几位。
    关闭表,程序中也不用m_pRecordset打开表,还有就是Excute()的_bstr_t类型参数要注意一下就好了。
    散分结贴。