当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: 在sql server中建立数据库,有一个名为 name的列, 用bde连接,为什么老是不能将其连结出来???而用ado却可以 表如下: student( id char(4); name char(10)......) ......
    摘要: 如何做出像在写bcb代码时,当输入"->"时弹出的对话框!? ......


在VC中,怎样实现将数据库的表中的一项数据(text类型)读出来在线等待,回复马上给分

在VC中,怎样实现将数据库的表中的一项数据读出来,我想将它显示到一个CEdit   控件中?

NO.1   作者: OpenVMS

example   ,write   text   field   to   file  
   
  #include   <afx.h>                                 //   MFC    
  #include   <iostream.h>                       //   iostream  
  #include   <stdlib.h>                           //   C   run-time  
   
  #if   defined   (_DEBUG)  
  #undef   THIS_FILE  
  static   char   BASED_CODE   THIS_FILE[]   =   __FILE__;  
  #endif  
   
  #define   DBNTWIN32                               //   Win32   DB-Library   for   Windows   NT  
  #include   <sqlfront.h>                       //   DB-Library  
  #include   <sqldb.h>                             //   DB-Library  
   
          char*   pszParam;                           //   gotten   parameter  
          int   nChunkSize   =   4096;                     //   chunk   size  
          CString   strChunkSize   =   "4096";     //   chunk   size  
          CString   strColumnType;  
   
          CString   strServer,                     //   SQL   Server  
                  strLogin,                               //   login  
                  strPassword,                         //   password  
                  strDatabase,                         //   database  
                  strTable,                               //   table  
                  strColumn,                             //   column  
                  strWhere,                               //   where   clause  
                  strFile;                                 //   file  
                   
          dberrhandle   (ErrorHandler);  
          dbmsghandle   (MessageHandler);  
   
          //   set   DB-Library   options  
          dbsettime(30);  
          dbsetlogintime(10);  
   
          //   get   login   record  
          PLOGINREC   pLoginRec;  
          pLoginRec   =   dblogin();  
          if   (pLoginRec   ==   NULL)  
          {  
                  cout   <<   err   <<   "Could   not   allocate   a   login   record"   <<   endl;  
                  return   (1);  
          }  
   
          //   fill   the   login   record   //------   assign   values.....  
          DBSETLUSER   (pLoginRec,   strLogin);               //   set   the   login  
          DBSETLPWD   (pLoginRec,   strPassword);           //   set   the   password  
          DBSETLAPP   (pLoginRec,   "textcopy");             //   set   the   app   name  
          DBSETLHOST   (pLoginRec,   "textcopy");           //   set   the   host   name  
          //   To   use   secure,   or   trusted,   connection,   uncomment   the   following   line.  
          //   DBSETLSECURE   (login);  
   
          //   attempt   to   connect   to   SQL   Server  
          PDBPROCESS   pDbproc;  
          pDbproc   =   dbopen   (pLoginRec,   strServer);  
          dbfreelogin   (pLoginRec);  
          if   (pDbproc   ==   NULL)  
          {  
                  cout   <<   err   <<   "Could   not   connect   to   SQL   Server   "   <<   strServer   <<   ""   <<   endl;  
                  return   (1);  
          }  
   
          //   re-used   DB-Library   return   code  
          RETCODE   r;  
   
          //   set   textlimit   and   textsize   options   for   this   connection  
                  dbsetopt   (pDbproc,   DBTEXTLIMIT,   strChunkSize);  
                  dbsetopt   (pDbproc,   DBTEXTSIZE,   "2147483647");  
                r   =   dbsqlexec   (pDbproc);  
                if   (r   ==   FAIL)  
                {  
                        cout   <<   err   <<   "Query   execution   failed."   <<   endl;  
                        Cleanup   (pDbproc);  
                        return   (1);  
                }  
         
                //   get   empty   result   set(s)   from   setting   options  
                while   (TRUE)  
                {  
                        r   =   dbresults   (pDbproc);  
                        if   (r   ==   FAIL)  
                        {  
                                cout   <<   err   <<   "Query   results   failed."   <<   endl;  
                                Cleanup   (pDbproc);  
                                return   (1);  
                        }  
                        if   (r   ==   NO_MORE_RESULTS)  
                                break;   //   while   loop  
                }  
         
                //   use   specified   database  
                if   (!strDatabase.IsEmpty())  
                {  
                        r   =   dbuse   (pDbproc,   strDatabase);  
                        if   (r   ==   FAIL)  
                        {  
                                cout   <<   err   <<   "Could   not   use   database   "   <<   strDatabase   <<   ""   <<   endl;  
                                Cleanup(pDbproc);  
                                return   (1);  
                        }  
                }  
         
                //   build   query  
                CString   strQuery;  
         
                strQuery   =   "select   "   +   strColumn   +   "   from   "   +   strTable   +   "   "   +   strWhere;  
                D(cout   <<   "Query:   "   <<   strQuery   <<   endl);  
         
                r   =   dbcmd   (pDbproc,   strQuery);  
         
                //   send   and   execute   query    
                r   =   dbsqlexec   (pDbproc);  
                if   (r   ==   FAIL)  
                {  
                        cout   <<   err   <<   "Query   execution   failed."   <<   endl;  
                        Cleanup   (pDbproc);  
                        return   (1);  
                }  
         
                //   get   first   result   set  
                r   =   dbresults   (pDbproc);  
                if   (r   !=   SUCCEED)  
                {  
                        cout   <<   err   <<   "Query   results   failed."   <<   endl;  
                        Cleanup   (pDbproc);  
                        return   (1);  
                }  
         
                //   verify   that   only   a   single   column   was   selected          
                if   (dbnumcols   (pDbproc)   !=   1)  
                {  
                        cout   <<   err   <<   "More   than   one   column   specified."   <<   endl;  
                        Cleanup   (pDbproc);  
                        return   (1);  
                }  
         
                //   verify   that   the   single   column   selected   is   either   text   or   image  
                int   nColumnType;  
                nColumnType   =   dbcoltype   (pDbproc,   1);  
                if   ((nColumnType   !=   SQLTEXT)   &&   (nColumnType   !=   SQLIMAGE))  
                {  
                        cout   <<   err   <<   "Specified   column   is   not   a   text   or   image   column."   <<   endl;  
                        Cleanup   (pDbproc);  
                        return   (1);  
                }  
                else  
                {  
                        if   (nColumnType   ==   SQLTEXT)  
                        {  
                                strColumnType   =   "text";  
                        }  
                        if   (nColumnType   ==   SQLIMAGE)  
                        {  
                                strColumnType   =   "image";  
                        }  
                }  
         
                //   buffer   for   data   transfer   between   DB-Library   and   file  
                aBuf   =   new   BYTE[nChunkSize];  
                if   (aBuf   ==   0)  
                {  
                        cout   <<   err   <<   "Unable   to   allocate   transfer   buffer   of   "   <<   nChunkSize   <<   "   bytes."   <<   endl;  
                        Cleanup   (pDbproc);  
                        return   (1);  
                }  
         
                //   if   the   data   is   coming   out   of   SQL   Server   and   into   a   file,   use   dbreadtext  
                //   (instead   of   dbnextrow)   to   read   the   text   or   image   data   from   the   row  
                //   in   chunks  
                 
          DBINT   lWriteBytes;  
                  while   (TRUE)  
                  {  
                          //   read   chunk   of   text   or   image   data   from   SQL   Server   into   aBuf  
                          lWriteBytes   =   dbreadtext   (pDbproc,   aBuf,   nChunkSize);  
                          switch   (lWriteBytes)  
                          {  
                          case   0:  
                                  //   end   of   text   or   image   row  
                                  D(cout   <<   "End   of   row"   <<   endl);  
                                  break;  
                          case   -1:  
                                  //   dbreadtext   failed  
                                  cout   <<   err   <<   "Text   or   image   data   retrieval   failed."   <<   endl;  
                                  Cleanup   (pDbproc);  
                                  return   (1);  
                                  break;  
                          case   NO_MORE_ROWS:  
                                  D(cout   <<   "No   more   rows"   <<   endl);  
                                  break;  
                          default:  
                                  //   dbreadtext   has   placed   lBytes   of   text   or   image   data  
                                  //   into   aBuf,   now   write   that   chunk   to   the   file  
                                //   file.Write   (aBuf,   lWriteBytes);//-------put   data   into   editbox  
                                   
                                  D(cout   <<   "Wrote   "   <<   lWriteBytes   <<   "   bytes   to   file"   <<   endl);  
                                  break;  
                          }  
                          if   ((lWriteBytes   ==   -1)   ||   (lWriteBytes   ==   NO_MORE_ROWS))  
                                  break;   //   while   loop  
   
              }  
 


    摘要: 在mysql中,一些常用的字段怎么定义最合理!? 一些常用的如id,姓名,年龄,电子信箱,发贴标题,发贴内容..之类的!? 欢迎赐教或讨论! ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE