vs2005 C++连接SQL2008数据库进行插入 删除 登陆查询

// sqlDlg.cpp : implementation file
//


#include "stdafx.h"
#include "sql.h"
#include "sqlDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// Dialog Data
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()




// CsqlDlg dialog








CsqlDlg::CsqlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CsqlDlg::IDD, pParent)
, name(_T(""))
, pass(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CsqlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, name);
DDX_Text(pDX, IDC_EDIT2, pass);
}


BEGIN_MESSAGE_MAP(CsqlDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CsqlDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CsqlDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CsqlDlg::OnBnClickedButton3)
END_MESSAGE_MAP()




// CsqlDlg message handlers


BOOL CsqlDlg::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 CsqlDlg::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 CsqlDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CsqlDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}




void CsqlDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if (name ==""||pass == "")
{
MessageBox(L"提示:请输入用户名和密码",0,0);
return ;
}
CoInitialize(NULL);
HRESULT hr=sqlSp.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
cout<<"_ConnectionPtr对象指针实例化失败!!!"<<endl;
return ;
}
else 
{
try 
{
_bstr_t strConnect="Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=1234567;database=DataCollection;";
sqlSp->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error &e)
{
// cout<<e.Description()<<endl;
string str = e.Description();
cout << "1   ";
cout<<str<<endl;
}

}

_RecordsetPtr m_pRecordset; //记录集对象指针,用来执行SQL语句并记录查询结果

if(FAILED(m_pRecordset.CreateInstance( _uuidof( Recordset ))))
{
AfxMessageBox("指针实例化失败 ");
return ;
}

CString strsql; 
strsql.Format(L"select UserType from t_User where UserName = '%s' and Password='%s'",name,pass);

m_pRecordset=sqlSp->Execute(_bstr_t(strsql),NULL,adCmdText); 

if (m_pRecordset->adoEOF)
{
MessageBox(L"登陆失败",0,0);
}
else
{
MessageBox(L"登陆成功",0,0);
}
//while(!m_pRecordset->adoEOF)
//{
// string UserName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("UserName"))->Value);
// string Password=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("Password"))->Value);
// cout<<UserName<<"  "<< Password<<endl;
// m_pRecordset->MoveNext();
//}

}


void CsqlDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if (name ==""||pass == "")
{
MessageBox(L"提示:请输入用户名和密码",0,0);
return ;
}
CoInitialize(NULL);
HRESULT hr=sqlSp.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
cout<<"_ConnectionPtr对象指针实例化失败!!!"<<endl;
return ;
}
else 
{
try 
{
_bstr_t strConnect="Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=1234567;database=DataCollection;";
sqlSp->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error &e)
{
// cout<<e.Description()<<endl;
string str = e.Description();
cout << "1   ";
cout<<str<<endl;
}
}
int a = 1;
CString strsql; 
strsql.Format(L"insert into t_User(UserName,Password) values('%s','%s')",name,pass);
m_pRecordset=sqlSp->Execute(_bstr_t(strsql),NULL,adCmdText); 
MessageBox(L"注册",0,0);
//m_pRecordset->Update();///保存到库中
MessageBox(L"注册1",0,0);
}


void CsqlDlg::OnBnClickedButton3()
{
// TODO: Add your control notification handler code here


UpdateData(TRUE);
if (name ==""||pass == "")
{
MessageBox(L"提示:请输入用户名和密码",0,0);
return ;
}
CoInitialize(NULL);
HRESULT hr=sqlSp.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
cout<<"_ConnectionPtr对象指针实例化失败!!!"<<endl;
return ;
}
else 
{
try 
{
_bstr_t strConnect="Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=1234567;database=DataCollection;";
sqlSp->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error &e)
{
// cout<<e.Description()<<endl;
string str = e.Description();
cout << "1   ";
cout<<str<<endl;
}
}
CString strsql; 
strsql.Format(L"delete from t_User where UserName = '%s'",name);  
m_pRecordset=sqlSp->Execute(_bstr_t(strsql),NULL,adCmdText); 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值