aotu modify table ' field's value,by ODBC directly is false,but use CDatabase can well and also the header file is needed.
//1:
header:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__A991CDF0_663C_4755_AC58_14C3A8EF4FE0__INCLUDED_)
#define AFX_STDAFX_H__A991CDF0_663C_4755_AC58_14C3A8EF4FE0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h> // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT
#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h> // MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endi![]()
2:teset odbc.
void CTESTDBSQLDlg::OnBUTTONTESTdb()
{
//here try use odbc directly to get objid.
//#include <SQL.H>
//#include <SQLEXT.H>
//ODBC32.LIBneed!!!!!!!1
RETCODE rcode;
HENV henv1;
HDBC hdbc1;
HSTMT hstmt1;
//char szFirstName[50];
char szLastName[50];
char field1[50];
char field2[50];
char field3[50];
unsigned char sql1[200]="update test2 set id1='345' where id1='3'";
//char szPhoneNum[20];
SDWORD sdODataLength;
unsigned char conStringOut[256];
//1:
rcode = ::SQLAllocEnv(&henv1);
if (rcode != SQL_SUCCESS) {
AfxMessageBox("allocEnvError.");
return;
}
if (rcode == SQL_SUCCESS)
{ //2:
rcode = ::SQLAllocConnect(henv1, & hdbc1);
if (rcode != SQL_SUCCESS) {
AfxMessageBox("AllocConnectError.");
return;
}
if (rcode == SQL_SUCCESS)
{ //3:
rcode = ::SQLDriverConnect(hdbc1, 0,
(unsigned char *)"DSN=JiSheng",
SQL_NTS, conStringOut, 256, NULL,
SQL_DRIVER_NOPROMPT);
if (rcode != SQL_SUCCESS) {
AfxMessageBox("DroverCpmmectEror.");
return;
}
if (rcode == SQL_SUCCESS)
{//4:
rcode = ::SQLAllocStmt(hdbc1, &hstmt1);
if (rcode != SQL_SUCCESS) {
AfxMessageBox("SQLAllocStmtEror.");
return;
}
if (rcode == SQL_SUCCESS)
{ //5:execute!
rcode = ::SQLExecDirect(hstmt1,
(unsigned char *)
//"update test2 set id1='34' where id1='3'",
"SELECT * FROM test2",
SQL_NTS);
if(rcode!=SQL_SUCCESS){
AfxMessageBox("exe error.");
}
//6:get result by return code.
for (rcode = ::SQLFetch(hstmt1); rcode == SQL_SUCCESS;rcode = SQLFetch(hstmt1))
{//here we test get one and exeute one.
//get field1:
::SQLGetData(hstmt1, 1, SQL_C_CHAR,
field1, 50, & sdODataLength);
//get field2:
::SQLGetData(hstmt1,2,SQL_C_CHAR,field2,50,&sdODataLength);
//char sql[200]="no";
//char field[100];
//strcat(field," and ");
//strcat(field,field2);
char fieldcom[50]="\0";
strcat(fieldcom,field1);
strcat(fieldcom,field2);
char sql[200]="\0";
strcat(sql,"update test2 set sql='");
strcat(sql,"'fieldcom'");
strcat(sql,"' ");
strcat(sql," where id1='");
strcat(sql,field1);
strcat(sql,"'\0");
//now exe update the selected one.
//-
rcode = ::SQLExecDirect(hstmt1,
(unsigned char *)
//"update test2 set sql='345' where id1='3'",
sql,//sql,//"update test2 set id1='"+fieldcom+"' where id1='"+field1+"'",
SQL_NTS);
if(rcode!=SQL_SUCCESS){
AfxMessageBox("exe error.");
}
//==
::MessageBox(NULL,sql,
" from AddressBookDb ", MB_OK);
}
::SQLFreeStmt(hstmt1, SQL_DROP);
}
::SQLDisconnect(hdbc1);
}
::SQLFreeConnect(hdbc1);
}
::SQLFreeEnv(henv1);
}
}
![]()
3:CDatabase:
void CTESTDBSQLDlg::OnTestDb2()
{
CDatabase db;
try{
CString strConn;
strConn.Format("ODBC;DSN=JiSheng;UID=;PWD=");
if(!db.Open(NULL,FALSE,FALSE,strConn,TRUE))
MessageBox("can not open db","info",MB_OK);
Test1Set set;
CString sql="select * from test2";
set.Open(AFX_DB_USE_DEFAULT_TYPE,sql);
CString insertSql;
CString f1;
CString f2;
CString com;
while(!set.IsEOF()){
f1=set.m_id1;
f2=set.m_id2;
com=f1+f2;
insertSql="update test2 set id1='"+com+"' where id1='"+f1+"'";
AfxMessageBox(insertSql);
db.ExecuteSQL(insertSql);
set.MoveNext();
}
//db.ExecuteSQL("");
db.Close();
}catch(CDBException e){
e.ReportError();
}
}
浙公网安备 33010602011771号