在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 << err << "Could not allocate a login record" << 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 << err << "Could not connect to SQL Server '" << strServer << "'" << 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 << err << "Query execution failed." << 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 << err << "Query results failed." << 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 << err << "Could not use database '" << strDatabase << "'" << endl;
97Cleanup(pDbproc);
98return (1);
99}
100}
101
102// build query
103CString strQuery;
104
105strQuery = "select " + strColumn + " from " + strTable + " " + strWhere;
106D(cout << "Query: " << strQuery << endl);
107
108r = dbcmd (pDbproc, strQuery);
109
110// send and execute query
111r = dbsqlexec (pDbproc);
112if (r == FAIL)
113{
114cout << err << "Query execution failed." << endl;
115Cleanup (pDbproc);
116return (1);
117}
118
119// get first result set
120r = dbresults (pDbproc);
121if (r != SUCCEED)
122{
123cout << err << "Query results failed." << endl;
124Cleanup (pDbproc);
125return (1);
126}
127
128// verify that only a single column was selected
129if (dbnumcols (pDbproc) != 1)
130{
131cout << err << "More than one column specified." << 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) && (nColumnType != SQLIMAGE))
140{
141cout << err << "Specified column is not a text or image column." << 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 << err << "Unable to allocate transfer buffer of '" << nChunkSize << "' bytes." << 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 << "End of row" << endl);
180break;
181case -1:
182// dbreadtext failed
183cout << err << "Text or image data retrieval failed." << endl;
184Cleanup (pDbproc);
185return (1);
186break;
187case NO_MORE_ROWS:
188D(cout << "No more rows" << 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 << "Wrote " << lWriteBytes << " bytes to file" << 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>