www.pudn.com > IVR_TEST.rar > 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 "msado15.dll" no_namespace rename("EOF","EndOfFile")
_ConnectionPtr conIVR; //定义一个连接变量
_RecordsetPtr rstIVR; //定义recordset
CString strReturn; //定义返回值
/////////////////////////////////////////////////////////////////////////////
// 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");
InitSQLServer("WANG","IVRLW","sa","");
//执行存贮过程,将值返回
desc=ExecStoreProc("11699881&11600&insert into tbl_100zx_t11699881_Operation (FOperationID,FMainID,FOperationCode,FManID,FCallNumber,FCallDate,FCountact,FKey,FisSend) values('11601','11111','1169988101','452122198305204510','0769-7986370','20041011','0769-7986370','46464f|545646',1)");
//desc=ExecStoreProc("11699881&11601&Select (Select SUM(FMoney) from tbl_100zx_t11699881_Account Where FWay=1 and FManID='45212219830520')-(Select SUM(FMoney) from tbl_100zx_t11699881_Account Where FWay=0 and FManID='45212219830520') as FBalance");
//desc=ExecStoreProc("11699881&11601&Select * from tbl_100zx_t11699881_Account");
//退出数据库
ExitDB();
//显示结果
AfxMessageBox(desc);
}
//初始化SQLServer数据库
int InitSQLServer(CString server,CString db,CString UserName,CString Pwd)
{
conIVR.CreateInstance("ADODB.Connection");
CString strCn;
strCn.Empty();
try
{
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);
//打开一个连接
conIVR->CursorLocation =adUseClient;
HRESULT hr=conIVR->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
//HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,"sa","mapper",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox("不能连接数据库 source!");
return -1;
}
}
catch(_com_error e)
{
AfxMessageBox("不能连接数据库 error!");
return -2;
}
return 0;
/*
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);
//打开一个连接
conIVR->CursorLocation =adUseClient;
conIVR->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
*/
}
//
//执行存贮过程,将值返回
//存贮过程创建于pubs数据库,代码在目录下的test存贮过程.txt中
//
CString ExecStoreProc(CString sMsg)
{
_CommandPtr cmdIVR;
rstIVR.CreateInstance("ADODB.Recordset");
// rstIVR->Cursorlocation=adUseClient;
cmdIVR.CreateInstance("ADODB.Command");
//id号
_ParameterPtr pParamRk;
pParamRk.CreateInstance("ADODB.Parameter");
pParamRk->Name="strInContentMsg"; //存储过程的参数1
pParamRk->Type=adChar; //整型
pParamRk->Size=500; //
pParamRk->Direction=adParamInput;//表明是输入参数
CString tstr;
tstr.Format("%s",sMsg);
pParamRk->Value=_variant_t(tstr);//int->CString->_variant_t后赋值
cmdIVR->Parameters->Append(pParamRk);
//cmd->Parameters->Append(pParamRk);
/*CString tstr;
tstr.Format("%d",id);
pParamRk->Value=_variant_t(tstr);//int->CString->_variant_t后赋值
cmd->Parameters->Append(pParamRk);*/
//返回值
_ParameterPtr pParamOk1;
pParamOk1.CreateInstance("ADODB.Parameter");
pParamOk1->Name="strReturnCode"; //参数2名称
pParamOk1->Type=adChar; //字符串
pParamOk1->Size=5; //大小为20个字节
pParamOk1->Direction=adParamOutput; //声明是输出参数
cmdIVR->Parameters->Append(pParamOk1);
_ParameterPtr pParamOk2;
pParamOk2.CreateInstance("ADODB.Parameter");
pParamOk2->Name="strReturnMsg"; //参数2名称
pParamOk2->Type=adChar; //字符串
pParamOk2->Size=50; //大小为20个字节
pParamOk2->Direction=adParamOutput; //声明是输出参数
cmdIVR->Parameters->Append(pParamOk2);
//_variant_t TheValue = MySet->GetCollect("FIELD_1");
//CString sValue=(char*)_bstr_t(TheValue);
cmdIVR->ActiveConnection = conIVR;
//cmdIVR->CommandText="SP_test"; //存储过程的名字
cmdIVR->CommandText="SP_11699881"; //存储过程的名字
cmdIVR->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
//执行,获得结果
rstIVR=cmdIVR->Execute(NULL, NULL, adCmdStoredProc);
long iRows=rstIVR->RecordCount;
CString strRows;
strRows.Format("rows:%d",iRows);
//AfxMessageBox(strRows);
long iCols=rstIVR->Fields->Count;
CString strCols;
strCols.Format("cols:%d",iCols);
AfxMessageBox(strCols);
//_variant_t
_bstr_t sValue;
sValue=rstIVR->GetString(adClipString, iRows, ",", "|", "");
char *buf=sValue;
strReturn=buf;
strReturn="|"+strReturn;
AfxMessageBox(strReturn);
/rstIVR->Close();
//将值返回
return strReturn;
// return (char*)_bstr_t(pParamOk1->Value);
}
//关闭数据库
void ExitDB()
{
if(conIVR!=NULL)
{
conIVR->Close();//关闭连接
conIVR.Release();//释放对象
}
}
void CTest1Dlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}