📄 odbc_testdlg.cpp
字号:
// ODBC_TESTDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ODBC_TEST.h"
#include "ODBC_TESTDlg.h"
#include <sql.h>
#include <sqlext.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CODBC_TESTDlg dialog
CODBC_TESTDlg::CODBC_TESTDlg(CWnd* pParent /*=NULL*/)
: CDialog(CODBC_TESTDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CODBC_TESTDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CODBC_TESTDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CODBC_TESTDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CODBC_TESTDlg, CDialog)
//{{AFX_MSG_MAP(CODBC_TESTDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON, OnButtonExcute)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CODBC_TESTDlg message handlers
BOOL CODBC_TESTDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetDlgItemText(IDC_EDIT_USERNAME, "sa");
SetDlgItemText(IDC_EDIT_PASSWORD, "sa");
SetDlgItemText(IDC_EDIT_SQL, "SELECT SNO, SNAME FROM S");
SetDlgItemText(IDC_EDIT_DSN, "student-sql");
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CODBC_TESTDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CODBC_TESTDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CODBC_TESTDlg::OnButtonExcute()
{
HENV henv = NULL;//句柄声名,此句柄为环境句柄
HDBC hdbc = NULL;//句柄声名,此句柄为连接句柄
HSTMT hstmt = NULL;//句柄声名
SQLINTEGER sNo, cbNo, cbName;
SQLCHAR szName[50];//主机名
SQLCHAR szDSN[100] ;//数据源名
SQLCHAR szSelect[1000];//SOL语句
SQLCHAR szUID[100]; //log name,是什么意思,
SQLCHAR szAuthStr[20]; //passward
::ZeroMemory(szName,sizeof(szName));//很想知道这一行是什么意思,有什么作用;
::ZeroMemory(szDSN,sizeof(szDSN));
::ZeroMemory(szSelect,sizeof(szSelect));
::ZeroMemory(szUID,sizeof(szUID));
::ZeroMemory(szAuthStr,sizeof(szAuthStr));
CString strTemp(_T(""));
strTemp.Empty();//将文本中的信息取下,赋于szUID,以下同理。
GetDlgItemText(IDC_EDIT_USERNAME, strTemp);
strcpy((char*)szUID, strTemp);
strTemp.Empty();
GetDlgItemText(IDC_EDIT_PASSWORD, strTemp);
strcpy((char*)szAuthStr, strTemp);
strTemp.Empty();
GetDlgItemText(IDC_EDIT_DSN, strTemp);
strcpy((char*)szDSN, strTemp);
strTemp.Empty();
GetDlgItemText(IDC_EDIT_SQL, strTemp);
strcpy((char*)szSelect, strTemp);
RETCODE retcode;
SQLAllocEnv(&henv); /*分配环境句柄*/ //
SQLAllocConnect(henv, &hdbc);/*分配连接句柄*/
CString strMsg(_T("")); //存放Message
//连接数据库
retcode=SQLConnect(hdbc,
(SQLCHAR*)szDSN, (SWORD)strlen((char*)szDSN),//数据源
(SQLCHAR*)szUID, (SWORD)strlen((char*)szUID),//用户名
(SQLCHAR*)szAuthStr, (SWORD)strlen((char*)szAuthStr)//登录密码
);
if((retcode == SQL_SUCCESS ) ||(retcode == SQL_SUCCESS_WITH_INFO))
{
TRACE0("Open database Succeed !\n") ; //TRACEO()是什么意思,有什么作用,和MessageBox()有什么区别。是不是都有提示作用。?
}
else
{
TRACE0("Open database Failed !\n") ;
/* Print the errmsg*/
strMsg.Format("Open database Failed !\n");
AfxMessageBox(strMsg);
SQLFreeEnv(henv); /*当完成应用后,释放环境句柄*/
return ;
}
SQLAllocStmt(hdbc,&hstmt); /*分配语句句柄*/
/*执行SQL语句*/
retcode = SQLExecDirect(hstmt,szSelect, SQL_NTS);
if (retcode == SQL_SUCCESS)
{
int iCount = 0;
/*结果集处理*/
while (TRUE)
{
retcode = SQLFetch(hstmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
iCount++;
/* Get data for columns 1, 2, and 3 */
SQLGetData(hstmt, 1, SQL_C_ULONG, &sNo, 0, &cbNo);
SQLGetData(hstmt, 2, SQL_C_CHAR, szName, 50, &cbName);
strMsg.Format("result record %d: %d, %s", iCount, sNo, szName);
AfxMessageBox(strMsg);//如果有多行语句,那不是要弹出多个Message(),为什么不用ListCtril
}
else
{
break;
}
}
}
else
{
strMsg.Format("SQLExecDirect Failed!");
AfxMessageBox(strMsg);
}
//释放语句句柄
if(hstmt != NULL)
{
SQLFreeStmt(hstmt,SQL_DROP) ;
hstmt = NULL ;
}
SQLDisconnect(hdbc); /*断开数据源*/
SQLFreeConnect(hdbc); /*释放连接句柄*/
SQLFreeEnv(henv); /*当完成应用后,释放环境句柄*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -