www.pudn.com > ADOExProc.zip > test1Dlg.cpp
// test1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test1.h"
#include "test1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
#import "f:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename("EOF","EndOfFile")
_ConnectionPtr piConnection; //定义一个连接变量
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog
CTest1Dlg::CTest1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest1Dlg)
// 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 CTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest1Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest1Dlg, CDialog)
//{{AFX_MSG_MAP(CTest1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg message handlers
BOOL CTest1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CTest1Dlg::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 CTest1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//
void CTest1Dlg::OnOK()
{
CString desc;
//输入服务器名、数据库名、用户名、密码
InitSQLServer("zrh","pubs","sa","zrhdjks");
//执行存贮过程,将值返回
desc=ExecStoreProc(10);
//退出数据库
ExitDB();
//显示结果
AfxMessageBox(desc);
}
//初始化SQLServer数据库
void InitSQLServer(CString server,CString db,CString UserName,CString Pwd)
{
piConnection.CreateInstance("ADODB.Connection");
CString strCn;
strCn.Empty();
strCn="provider=SQLOLEDB;data source="+server
+";initial catalog="+db
+";userID="+UserName
+";Password="+Pwd;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(UserName);
_variant_t bpwdstr=_variant_t(Pwd);
//打开一个连接
piConnection->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
}
//
//执行存贮过程,将值返回
//存贮过程创建于pubs数据库,代码在目录下的test存贮过程.txt中
//
CString ExecStoreProc(int id)
{
_CommandPtr cmd;
cmd.CreateInstance("ADODB.Command");
//id号
_ParameterPtr pParamRk;
pParamRk.CreateInstance("ADODB.Parameter");
pParamRk->Name="job_id"; //存储过程的参数1
pParamRk->Type=adInteger; //整型
pParamRk->Size=2; //
pParamRk->Direction=adParamInput;//表明是输入参数
CString tstr;
tstr.Format("%d",id);
pParamRk->Value=_variant_t(tstr);//int->CString->_variant_t后赋值
cmd->Parameters->Append(pParamRk);
//返回值
_ParameterPtr pParamOk;
pParamOk.CreateInstance("ADODB.Parameter");
pParamOk->Name="desc"; //参数2名称
pParamOk->Type=adChar; //字符串
pParamOk->Size=20; //大小为20个字节
pParamOk->Direction=adParamOutput; //声明是输出参数
cmd->Parameters->Append(pParamOk);
//_variant_t TheValue = MySet->GetCollect("FIELD_1");
//CString sValue=(char*)_bstr_t(TheValue);
cmd->ActiveConnection = piConnection;
cmd->CommandText="test"; //存储过程的名字
cmd->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
//执行,获得结果
cmd->Execute(NULL, NULL, adCmdStoredProc);
//将值返回
return (char*)_bstr_t(pParamOk->Value);
}
//关闭数据库
void ExitDB()
{
if(piConnection!=NULL)
{
piConnection->Close();//关闭连接
piConnection.Release();//释放对象
}
}