已连接上access数据表,在list control(变量名m_Grid)中显示其数据。现在要给数据表添加字段,字段名通过edit control(变量名m_FieldName)输入。
(用excute执行sql语句来解决)。并且list control能显示新添加的字段。下面是我得代码。
        
         UpdateData(TRUE);
if(m_FieldName.IsEmpty())
{
MessageBox("请输入要添加的字段名!");
return;
}
      OnInitADOConn();
 
  CString str;
  str=m_FieldName;
  bstr_t bstrSQL;
bstrSQL="alter table employees add   str  int ";  //这里需要修改
 m_pCommand.CreateInstance (_uuidof(Command));
 try   
  {   
     m_pCommand->ActiveConnection   =   m_pConnection;   
     m_pCommand->CommandText   =   (_bstr_t)bstrSQL;   
     m_pCommand->Execute(NULL,   NULL,   adCmdText);   
  }   
  catch(_com_error   e)   
  {   
    AfxMessageBox(e.Description()); 
  }
  m_Grid.InsertColumn(4,str,LVCFMT_LEFT,110,3);//这里需要修改我现在的问题是,sql语句不正确,数据库不能添加编辑框中输入的字段,请教各位此处怎么修改?谢谢了
另外list框的更新显示是不是有更简单有效的方法。
我刚接触vc,不通。还望各位不吝指教!不胜感谢。

解决方案 »

  1.   

    刚接触就玩这么深
    不建议使用COM对数据库表进行字段修改
      

  2.   


    SQL 语句分为 DDL 和 DCL。
    你这个语句属于 DDL。看下边这篇文章:
    How to use common Data Definition Language (DDL) SQL statements for the Jet database engine
    http://support.microsoft.com/?scid=kb;en-us;180841&x=7&y=8
      

  3.   

    这样修改:
    CString strSQL; 
    strSQL.Format("alter table employees add %s int",m_FieldName);
    ...
    m_pCommand->CommandText = (_bstr_t)strSQL;