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

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

example ,write text field to file

#include

  1<afx.h> // MFC   
  2#include <iostream.h> // iostream   
  3#include <stdlib.h> // C run-time   
  4  
  5#if defined (_DEBUG)   
  6#undef THIS_FILE   
  7static char BASED_CODE THIS_FILE[] = __FILE__;   
  8#endif   
  9  
 10#define DBNTWIN32 // Win32 DB-Library for Windows NT   
 11#include <sqlfront.h> // DB-Library   
 12#include <sqldb.h> // DB-Library   
 13  
 14char* pszParam; // gotten parameter   
 15int nChunkSize = 4096; // chunk size   
 16CString strChunkSize = "4096"; // chunk size   
 17CString strColumnType;   
 18  
 19CString strServer, // SQL Server   
 20strLogin, // login   
 21strPassword, // password   
 22strDatabase, // database   
 23strTable, // table   
 24strColumn, // column   
 25strWhere, // where clause   
 26strFile; // file   
 27  
 28dberrhandle (ErrorHandler);   
 29dbmsghandle (MessageHandler);   
 30  
 31// set DB-Library options   
 32dbsettime(30);   
 33dbsetlogintime(10);   
 34  
 35// get login record   
 36PLOGINREC pLoginRec;   
 37pLoginRec = dblogin();   
 38if (pLoginRec == NULL)   
 39{   
 40cout &lt;&lt; err &lt;&lt; "Could not allocate a login record" &lt;&lt; endl;   
 41return (1);   
 42}   
 43  
 44// fill the login record //------ assign values.....   
 45DBSETLUSER (pLoginRec, strLogin); // set the login   
 46DBSETLPWD (pLoginRec, strPassword); // set the password   
 47DBSETLAPP (pLoginRec, "textcopy"); // set the app name   
 48DBSETLHOST (pLoginRec, "textcopy"); // set the host name   
 49// To use secure, or trusted, connection, uncomment the following line.   
 50// DBSETLSECURE (login);   
 51  
 52// attempt to connect to SQL Server   
 53PDBPROCESS pDbproc;   
 54pDbproc = dbopen (pLoginRec, strServer);   
 55dbfreelogin (pLoginRec);   
 56if (pDbproc == NULL)   
 57{   
 58cout &lt;&lt; err &lt;&lt; "Could not connect to SQL Server '" &lt;&lt; strServer &lt;&lt; "'" &lt;&lt; endl;   
 59return (1);   
 60}   
 61  
 62// re-used DB-Library return code   
 63RETCODE r;   
 64  
 65// set textlimit and textsize options for this connection   
 66dbsetopt (pDbproc, DBTEXTLIMIT, strChunkSize);   
 67dbsetopt (pDbproc, DBTEXTSIZE, "2147483647");   
 68r = dbsqlexec (pDbproc);   
 69if (r == FAIL)   
 70{   
 71cout &lt;&lt; err &lt;&lt; "Query execution failed." &lt;&lt; endl;   
 72Cleanup (pDbproc);   
 73return (1);   
 74}   
 75  
 76// get empty result set(s) from setting options   
 77while (TRUE)   
 78{   
 79r = dbresults (pDbproc);   
 80if (r == FAIL)   
 81{   
 82cout &lt;&lt; err &lt;&lt; "Query results failed." &lt;&lt; endl;   
 83Cleanup (pDbproc);   
 84return (1);   
 85}   
 86if (r == NO_MORE_RESULTS)   
 87break; // while loop   
 88}   
 89  
 90// use specified database   
 91if (!strDatabase.IsEmpty())   
 92{   
 93r = dbuse (pDbproc, strDatabase);   
 94if (r == FAIL)   
 95{   
 96cout &lt;&lt; err &lt;&lt; "Could not use database '" &lt;&lt; strDatabase &lt;&lt; "'" &lt;&lt; endl;   
 97Cleanup(pDbproc);   
 98return (1);   
 99}   
100}   
101  
102// build query   
103CString strQuery;   
104  
105strQuery = "select " + strColumn + " from " + strTable + " " + strWhere;   
106D(cout &lt;&lt; "Query: " &lt;&lt; strQuery &lt;&lt; endl);   
107  
108r = dbcmd (pDbproc, strQuery);   
109  
110// send and execute query   
111r = dbsqlexec (pDbproc);   
112if (r == FAIL)   
113{   
114cout &lt;&lt; err &lt;&lt; "Query execution failed." &lt;&lt; endl;   
115Cleanup (pDbproc);   
116return (1);   
117}   
118  
119// get first result set   
120r = dbresults (pDbproc);   
121if (r != SUCCEED)   
122{   
123cout &lt;&lt; err &lt;&lt; "Query results failed." &lt;&lt; endl;   
124Cleanup (pDbproc);   
125return (1);   
126}   
127  
128// verify that only a single column was selected   
129if (dbnumcols (pDbproc) != 1)   
130{   
131cout &lt;&lt; err &lt;&lt; "More than one column specified." &lt;&lt; endl;   
132Cleanup (pDbproc);   
133return (1);   
134}   
135  
136// verify that the single column selected is either text or image   
137int nColumnType;   
138nColumnType = dbcoltype (pDbproc, 1);   
139if ((nColumnType != SQLTEXT) &amp;&amp; (nColumnType != SQLIMAGE))   
140{   
141cout &lt;&lt; err &lt;&lt; "Specified column is not a text or image column." &lt;&lt; endl;   
142Cleanup (pDbproc);   
143return (1);   
144}   
145else   
146{   
147if (nColumnType == SQLTEXT)   
148{   
149strColumnType = "text";   
150}   
151if (nColumnType == SQLIMAGE)   
152{   
153strColumnType = "image";   
154}   
155}   
156  
157// buffer for data transfer between DB-Library and file   
158aBuf = new BYTE[nChunkSize];   
159if (aBuf == 0)   
160{   
161cout &lt;&lt; err &lt;&lt; "Unable to allocate transfer buffer of '" &lt;&lt; nChunkSize &lt;&lt; "' bytes." &lt;&lt; endl;   
162Cleanup (pDbproc);   
163return (1);   
164}   
165  
166// if the data is coming out of SQL Server and into a file, use dbreadtext   
167// (instead of dbnextrow) to read the text or image data from the row   
168// in chunks   
169  
170DBINT lWriteBytes;   
171while (TRUE)   
172{   
173// read chunk of text or image data from SQL Server into aBuf   
174lWriteBytes = dbreadtext (pDbproc, aBuf, nChunkSize);   
175switch (lWriteBytes)   
176{   
177case 0:   
178// end of text or image row   
179D(cout &lt;&lt; "End of row" &lt;&lt; endl);   
180break;   
181case -1:   
182// dbreadtext failed   
183cout &lt;&lt; err &lt;&lt; "Text or image data retrieval failed." &lt;&lt; endl;   
184Cleanup (pDbproc);   
185return (1);   
186break;   
187case NO_MORE_ROWS:   
188D(cout &lt;&lt; "No more rows" &lt;&lt; endl);   
189break;   
190default:   
191// dbreadtext has placed lBytes of text or image data   
192// into aBuf, now write that chunk to the file   
193// file.Write (aBuf, lWriteBytes);//-------put data into editbox   
194  
195D(cout &lt;&lt; "Wrote " &lt;&lt; lWriteBytes &lt;&lt; " bytes to file" &lt;&lt; endl);   
196break;   
197}   
198if ((lWriteBytes == -1) ¦ ¦ (lWriteBytes == NO_MORE_ROWS))   
199break; // while loop   
200  
201}</sqldb.h></sqlfront.h></stdlib.h></iostream.h></afx.h>
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus