您的位置:程序门 -> vc/mfc -> 基础类



ado连接access时候,取表的字段名字,老是出现异常!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


ado连接access时候,取表的字段名字,老是出现异常![已结贴,结贴人:xyliang230]
发表于:2007-08-15 18:50:09 楼主
我用ado连接access时候,取表的字段名字
m_recordset-> fields-> getitem(_variant_t(k))-> getname()
执行这句时候就是出现异常.

database.exe   中的   0x7c812a5b   处未处理的异常:   microsoft   c++   异常:   内存位置   0x0012f65c   处的   _com_error。

int   k为字段的位置(这没有问题),数据库连接也没有问题,m_recordset.getcollect()也没有问题,就是取字段名称出问题.
请问怎么回事?谢谢!    
发表于:2007-08-15 19:06:481楼 得分:0
刚才群里面的问题似乎也是你提的   呵呵
发表于:2007-08-15 19:18:472楼 得分:0
是的,是不是问题太简单大家都不愿意回答,还是我的错误比较低级.就是没有人愿意指点一二.
发表于:2007-08-15 19:38:263楼 得分:20

_variant_t   vindex;

vindex.ival   =   k;

bstr   =   prs-> getfields()-> getitem(&vindex)-> getname();


vc++   6.0     测试通过。
发表于:2007-08-15 19:41:194楼 得分:0
我的blog里面有代码!
发表于:2007-08-15 19:48:365楼 得分:0
谢谢zaodt(至尊宝宝:我就在你身旁,关心永远不打烊!)

  我的vs2005,还是不行,错误   依旧!
发表于:2007-08-15 20:26:306楼 得分:0
字段名是随机生成的,也只有一个个试才可以知道   .
发表于:2007-08-15 20:43:307楼 得分:0
k的位置是从0开始计数的。
发表于:2007-08-15 21:40:538楼 得分:0
是从零开始的
发表于:2007-08-16 08:09:419楼 得分:10
是不是k需要进行强制类型转换?
发表于:2007-08-16 09:03:3610楼 得分:0
跟lz一起学习
发表于:2007-08-16 09:30:3811楼 得分:0
该不会是前面就出错了吧。。。。
发表于:2007-08-16 09:35:0112楼 得分:0
我设置断点跟踪的,前面应该是没有问题的
发表于:2007-08-16 10:33:0413楼 得分:0
我用zaodt(至尊宝宝:我就在你身旁,关心永远不打烊!)的方法也不行:(
我的也是vc6.0
发表于:2007-08-16 10:53:3714楼 得分:20
知道了,知道了!哈哈,试了好多次,最后参考msdn找到的
把k的类型改成short就可以啦!哈哈哈哈
发表于:2007-08-16 10:55:5115楼 得分:0
msdn的示例代码如下,我原来用的是int,其他的都和示例基本一样,可就是不成,最后发现是类型不同,呵呵,人家用的是short,俺用的是int或者long,哈哈

void   itemx(void)  
{
        hresult     hr   =   s_ok;

        //   define   ado   object   pointers.
        //   initialize   pointers   on   define.
        //   these   are   in   the   adodb::     namespace.
      _connectionptr     pconnection     =   null;
      _recordsetptr       prst           =   null;
      _commandptr           pcmd           =   null;
      _parameterptr       pprm           =   null;
        fieldptr               pfld           =   null;

        //   other   variables.
        _bstr_t   strcnn( "provider=sqloledb;data   source=myserver; "
                        "initial   catalog=pubs;user   id=sa;password=; ");
        _variant_t         column[9];
        _variant_t         vindex;

      try
        {
              //   open   connection.
                testhr(pconnection.createinstance(__uuidof(connection)));

                testhr(prst.createinstance(__uuidof(recordset)));

                testhr(pcmd.createinstance(__uuidof(command)));

                //set   the   array   with   the   authors   table   field   (column)   names
                column[0]   =   "au_id ";
                column[1]   =   "au_lname ";
                column[2]   =   "au_fname ";
                column[3]   =   "phone ";
                column[4]   =   "address ";
                column[5]   =   "city ";
                column[6]   =   "state ";
                column[7]   =   "zip ";
                column[8]   =   "contract ";

                _bstr_t   strtext( "select   *   from   authors   where   state   =   ? ");
                pcmd-> commandtext   =   strtext;

                pprm   =   pcmd-> createparameter( "itemxparm ",   adchar,   adparaminput,
                        2,   "ca ");
                pcmd-> parameters-> append(pprm);

                pconnection-> open(strcnn,   " ",   " ",   adconnectunspecified);
                pcmd-> activeconnection   =   pconnection;

                //   connection   and   commandtype   are   omitted   because  
                //   a   command   object   is   provided.
                _variant_t   conn;
                conn.vt   =   vt_error;
                conn.scode   =   disp_e_paramnotfound;

                prst-> open((_variant_t((idispatch   *)   pcmd)),conn,adopenstatic,
                        adlockreadonly,   -1);

                printf( "the   parameters   collection   accessed   by   index...\n ");

                vindex   =   (short)   0;
                pprm   =   pcmd-> parameters-> getitem(&vindex);
                printf( "parameter   name   =   '%s ',   value   =   '%s '\n ",
                        (lpcstr)pprm-> name,   (lpstr)(_bstr_t)pprm-> value);

                printf( "\nthe   parameters   collection   accessed   by   name...\n ");
                pprm   =   pcmd-> parameters-> item[ "itemxparm "];
                printf( "parameter   name   =   '%s ',   value   =   '%s '\n ",
                        (lpcstr)pprm-> name,   (lpstr)(_bstr_t)pprm-> value);

                printf( "\nthe   fields   collection   accessed   by   index...\n ");
                prst-> movefirst();
                long   limit   =   0;
                limit   =   ((prst-> fields-> count)   -   1);
                int   intlinecnt   =   8;  
                for   (short   iindex   =   0;   iindex   <=   limit;   iindex++)
                {
                        vindex   =   iindex;
                        intlinecnt++;
                        if   (intlinecnt%15   ==   0)
                        {
                                printf( "\npress   any   key   to   continue...\n ");
                                getch();
                        }
                        pfld   =   prst-> fields-> getitem(&vindex);
                        printf( "field   %d   :   name   =     '%s ',   ",   iindex,
                                (lpcstr)pfld-> getname());
                        _variant_t   fldval   =   pfld-> getvalue();    

                        //   because   value   is   the   default   property   of   a
                        //   property   object,the   use   of   the   actual   keyword
                        //   here   is   optional.
                        switch(fldval.vt)
                        {
                                case   (vt_bool):
                                        if(fldval.boolval)
                                        {
                                                printf( "value   =   'true ' ");
                                        }
                                        else
                                        {
                                                printf( "value   =   'false ' ");
                                        }
                                        printf( "\n ");
                                        break;
                                case   (vt_bstr):
                                        printf( "value   =   '%s ' ",
                                                (lpcstr)(_bstr_t)fldval.bstrval);
                                        printf( "\n ");
                                        break;
                                case   (vt_i4):
                                        printf( "value   =   '%s ' ",(lpcstr)fldval.ival);
                                        printf( "\n ");
                                        break;
                                case   (vt_empty):
                                        printf( "value   =   '%s ' ",(lpcstr)fldval.lval);
                                        printf( "\n ");
                                        break;
                                default:
                                        break;
                        }
                }

                printf( "\nthe   fields   collection   accessed   by   name...\n ");
                prst-> movefirst();
                for   (iindex   =   0;   iindex   <=   8;   iindex++)
                {
                        intlinecnt++;
                        if   (intlinecnt%15   ==   0)
                        {
                                printf( "\npress   any   key   to   continue...\n ");
                                getch();
                        }
                        pfld   =   prst-> fields-> getitem(column[iindex]);
                        printf( "field   name   =   '%s ',   ",(lpcstr)pfld-> getname());
                        _variant_t   fldval   =   pfld-> getvalue();

                        //   because   value   is   the   default   property   of   a
                        //   property   object,the   use   of   the   actual   keyword
                        //   here   is   optional.
                        switch(fldval.vt)
                        {
                                case   (vt_bool):
                                        if(fldval.boolval)
                                        {
                                                printf( "value   =   'true ' ");
                                        }
                                        else
                                        {
                                                printf( "value   =   'false ' ");
                                        }
                                        printf( "\n ");
                                        break;
                                case   (vt_bstr):
                                        printf( "value   =   '%s ' ",
                                                (lpcstr)(_bstr_t)fldval.bstrval);
                                        printf( "\n ");
                                        break;
                                case   (vt_i4):
                                        printf( "value   =   '%s ' ",(lpcstr)fldval.ival);
                                        printf( "\n ");
                                        break;
                                case   (vt_empty):
                                        printf( "value   =   '%s ' ",(lpcstr)fldval.lval);
                                        printf( "\n ");
                                        break;
                                default:
                                        break;
                        }
                }
                //   clean   up   objects   before   exit.
                prst-> close();
                pconnection-> close();
      }

      catch(_com_error   &e)
      {
              //   notify   the   user   of   errors   if   any.
              //   pass   a   connection   pointer   accessed   from   the   recordset.
              printprovidererror(pconnection);
              printcomerror(e);
      }
}
发表于:2007-08-16 16:13:1716楼 得分:0
搞定,结贴!


快速检索

最新资讯
热门点击