try
{
m_spConnection.CreateInstance( __uuidof(Connection) );
/* m_spConnection->Open(
_T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Demo.accdb"),
_T(""),
_T(""),
adModeUnknown );*/

m_spConnection->Open
(_T("driver={SQL Server};Server=127.0.0.1;DATABASE=;UID=sa;PWD=123"),"","",/*0*/adConnectUnspecified);

}
红色部分是原来的连接代码,绿色的是我改后的,
程序运行后(用绿色的)库中的记录都可显示,但添加记录时会出现Idispatch error #3092错误,但用原来的连接方式确不会出错,下面是添加记录代码
CString strName;
m_editName.GetWindowText( strName );
if( strName.IsEmpty() || strName.GetLength() >= 50 )
{
MessageBox(_T("姓名不可以为空或者姓名太长!") );
return;
} CString strAge;
m_editAge.GetWindowText( strAge );
if( strAge.IsEmpty() )
{
MessageBox( _T("年龄不可为空") );
return;
} int iAge = _tstoi( (LPCTSTR)strAge );
if( iAge<0 || iAge > 150 )
{
MessageBox( _T("年龄输入不合法:太大或太小") );
return;
} // 添加到数据库中去
CString strCmd;
strCmd.Format( _T("insert into demo values( \"%s\", %d )"),
strName,
iAge ); _CommandPtr spCmd;
spCmd.CreateInstance( __uuidof(Command) );
spCmd->ActiveConnection = this->m_spConnection;
spCmd->CommandText = (LPCTSTR)strCmd;
try
{
spCmd->Execute( NULL, NULL, adCmdText );
}
catch( _com_error& e )
{
MessageBox( e.ErrorMessage() );
return;
}

谁明白给指出错误的地方

解决方案 »

  1.   

    ADODB::_RecordsetPtr m_spRecordset;
    m_spRecordset.CreateInstance( __uuidof( ADODB::Recordset) );
    m_spRecordset = m_spConnection->Execute( bstrSQL, NULL, ADODB::adCmdText) ;用这个试一试
      

  2.   

    ADODB::这个是哪来的?没定义呀
      

  3.   


    m_pCon.CreateInstance(__uuidof(Connection));   // 创建连接实例,注意是两条”_”
    m_pCon->Open("Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=XSCJ;Data Source= DELL-CRAZY","","",0);          // 设置连接字符串
      

  4.   

     m_pCon.CreateInstance(__uuidof(Connection));      是两条”_”呀,编译能通过,且能运行,刷新代码也好使(能把库中记录显示在列表中),就是删除记录和添加记录的代码出错
      

  5.   

    你把你的SQL语句放在 SQLserver里面 直接执行一下, 看嫩不能成功
      

  6.   

    我的连接字符串时这样写的,在工程里面用的一直没问题, 你改一下试试Provider=SQLOLEDB; Data Source=服务器地址; Initial Catalog=数据库名称; User ID=sa; Password=123;
      

  7.   

    呵呵,解决了,("insert into demo values( \"%s\", %d )"), 去掉转义字符就好使了,但是连access时没出错一切都正常换成sqlserver就出错,唉,不知为什么,也许是更严格了一些吧
    多谢馒头的帮助,呵呵