在VS的MFC单文档工程中加入SQL数据库(二)———用户注册

https://blog.csdn.net/GMLLYY/article/details/79704912

接上篇。。。

在所创建的单文档工程的登陆对话框上添加 注册用户(用Staic text空间)

注意,需要在其属性列表里将Notify的属性改为true后,双击注册用户才可进入代码编辑,代码如下:

void CLog::OnStnClickedZhuce() //注册用户的ID为IDC_ZhuCe

{
// TODO:  在此添加控件通知处理程序代码
INT_PTR nRes;             // 用于保存DoModal函数的返回值   
CLogIn tipDlg;           // 构造对话框类CTipDlg的实例   
nRes = tipDlg.DoModal();  // 弹出对话框   
if (IDCANCEL == nRes)     // 判断对话框退出后返回值是否为IDCANCEL,如果是则return,否则继续向下执行   
return;
// 将各控件中的数据保存到相应的变量   
UpdateData(TRUE);
}
然后在所创建的单文档工程中添加一个新的对话框,在对话框上添加控件,如下图所示:

控件添加后,对示例编辑框进行添加变量,依然为String类型

仍然需要将DDX_Control改为DDX_Text,如下图:

然后双击确定按钮,进入编辑,代码如下:

void CLogIn::OnBnClickedOk()
{
// TODO:  在此添加控件通知处理程序代码
UpdateData(TRUE);
int length = m_newPassword.GetLength();
if (length>12 || length<8)
{
MessageBox("密码设置长度不符合");
}
else if (m_newName == "")
{
MessageBox("用户名不能为空");
}
else if (m_newPassword == m_newPPassword)
{
CoInitialize(NULL); //初始化COM组件
_ConnectionPtr pConn(__uuidof(Connection)); //实例化一个connection对象pConn,连接对象
_RecordsetPtr pRst(__uuidof(Recordset)); //实例化一个Recordset对象pRst,记录集对象
_CommandPtr pCmd(__uuidof(Command)); //实例化一个Command对象pCmd,命令对象
try
{
pConn->ConnectionString = "Provider = SQLOLEDB.1;Persist Security Info = False;User ID = sa ;Password =123;Initial Catalog =Doctors;Data Source=DESKTOP-KKT1HG5";    //连接数据库
pConn->Open("", "", "", adConnectUnspecified);//打开数据库。此处参数均已在上述字符串声明,故可为空
}
catch (_com_error e)                          //捕捉异常
{
AfxMessageBox(e.Description());
}
CString strSql;
strSql.Format("Select * from Doctor_logon where username='%s'", m_newName);
pRst = pConn->Execute(_bstr_t(strSql), NULL, adCmdText);//将查询数据导入m_pRecordset数据容器
if (!pRst->adoEOF)                //查询
{
MessageBox("该用户已经存在");  //进入主窗体并关闭登录框
m_newName = "";
m_newPassword = "";
m_newPPassword = "";
UpdateData(FALSE);
}
else
{
CString str_Sql1, str_Sql2, str_Sql3, str_Sql4;
str_Sql1.Format("insert into Doctor_logon (username,password) values('%s','%s')", m_newName, m_newPassword);
pRst = pConn->Execute(_bstr_t(str_Sql1), NULL, adCmdText);//将查询数据导入m_pRecordset数据容器 
str_Sql2.Format("create table [" + m_newName + "] (病历号 nvarchar(50),姓名 nvarchar(50),性别 nvarchar(50),年龄 nvarchar(50),身高 nvarchar(50),体重 nvarchar(50),联系电话 nvarchar(50),临床诊断 nvarchar(50),备注 nvarchar(50))");
pRst = pConn->Execute(_bstr_t(str_Sql2), NULL, adCmdText);//将查询数据导入m_pRecordset数据容器
MessageBox("注册成功");
CLogIn::OnOK();
}
//pRst->Close(); //关闭对象
pConn->Close();
pCmd.Release(); //释放对象
pRst.Release();
pConn.Release();
CoUninitialize();


}


else


MessageBox("两次密码输入有误");
m_newPassword = "";
m_newPPassword = "";
UpdateData(FALSE);

}

点击运行,即可进行注册,然后通过注册的用户名和密码就可以登陆了。
 

猜你喜欢

转载自blog.csdn.net/active2489595970/article/details/88241714
今日推荐