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 }
#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 }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货