VC SQL 登录对话框数据库对用户名和密码进行验证代码!
2015-01-15 15:34
656 查看
该代码是一个完整的函数。包含连接数据库模块,和用户名密码输入为空判断,以及数据库验证是否该用户名密码存在?
void CDL::OnLoad()
{
//初始化操作
::CoInitialize(NULL);
//创建ADO连接操作
//AfxOleInit();
HRESULT hr;
//此处是通过adoconnect类的对象adoconnect来调用自己的成员对象
hr = adoconnect.m_pConnection.CreateInstance(_uuidof(Connection));
//判断是否为空
if(FAILED(hr))
{
AfxMessageBox("字符数据为空");
return;
}
try
{
//打开本地数据库student
CString str = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=QQ";
//打开数据库
adoconnect.m_pConnection->Open((_bstr_t)str,"","",-1);
//测试是否连接成功
AfxMessageBox("数据库连接成功!!");
}
//异常捕获
catch(_com_error *e)
{
AfxMessageBox("数据库连接失败!!");
AfxMessageBox(e->ErrorMessage());
return;
}
/*以上部分为数据库链接模块*/
CString strvalue1,strvalue2;
CString str_username,str_password,str_sql_user;
_variant_t var1,var2;
GetDlgItem(IDC_EDIT5)->GetWindowText(str_username);
GetDlgItem(IDC_EDIT6)->GetWindowText(str_password);
if (str_username.IsEmpty()||str_password.IsEmpty())
{
AfxMessageBox(TEXT("用户名、密码不能为空!"));
return;
}
str_sql_user="select * from users";//将控件变量传入sql执行语句中
adoconnect.m_pRecordset=adoconnect.GetRecordSet((_bstr_t)str_sql_user);
//判断是否到记录集尾部。
while(!adoconnect.m_pRecordset->adoEOF)
{
var1 = adoconnect.m_pRecordset->GetCollect("user");
var2 = adoconnect.m_pRecordset->GetCollect("password");
strvalue1 = (LPCSTR)_bstr_t(var1);
strvalue2 = (LPCSTR)_bstr_t(var2);
strvalue1.Replace(' ',NULL); //由于数据库在设计时,字符串长度设置为10,不够时空格填补,此处应该去空格
strvalue2.Replace(' ',NULL);
if(str_username!=""&&str_password!="")
{
if(str_username==strvalue1&&str_password==strvalue2)
{
AfxMessageBox("用户登录成功!");
return;
}
else
{
// remember++;
//return;
}
}
adoconnect.m_pRecordset->MoveNext();
}
AfxMessageBox("对不起,您输入的用户名或密码不正确,请重新输入!");
}
void CDL::OnLoad()
{
//初始化操作
::CoInitialize(NULL);
//创建ADO连接操作
//AfxOleInit();
HRESULT hr;
//此处是通过adoconnect类的对象adoconnect来调用自己的成员对象
hr = adoconnect.m_pConnection.CreateInstance(_uuidof(Connection));
//判断是否为空
if(FAILED(hr))
{
AfxMessageBox("字符数据为空");
return;
}
try
{
//打开本地数据库student
CString str = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=QQ";
//打开数据库
adoconnect.m_pConnection->Open((_bstr_t)str,"","",-1);
//测试是否连接成功
AfxMessageBox("数据库连接成功!!");
}
//异常捕获
catch(_com_error *e)
{
AfxMessageBox("数据库连接失败!!");
AfxMessageBox(e->ErrorMessage());
return;
}
/*以上部分为数据库链接模块*/
CString strvalue1,strvalue2;
CString str_username,str_password,str_sql_user;
_variant_t var1,var2;
GetDlgItem(IDC_EDIT5)->GetWindowText(str_username);
GetDlgItem(IDC_EDIT6)->GetWindowText(str_password);
if (str_username.IsEmpty()||str_password.IsEmpty())
{
AfxMessageBox(TEXT("用户名、密码不能为空!"));
return;
}
str_sql_user="select * from users";//将控件变量传入sql执行语句中
adoconnect.m_pRecordset=adoconnect.GetRecordSet((_bstr_t)str_sql_user);
//判断是否到记录集尾部。
while(!adoconnect.m_pRecordset->adoEOF)
{
var1 = adoconnect.m_pRecordset->GetCollect("user");
var2 = adoconnect.m_pRecordset->GetCollect("password");
strvalue1 = (LPCSTR)_bstr_t(var1);
strvalue2 = (LPCSTR)_bstr_t(var2);
strvalue1.Replace(' ',NULL); //由于数据库在设计时,字符串长度设置为10,不够时空格填补,此处应该去空格
strvalue2.Replace(' ',NULL);
if(str_username!=""&&str_password!="")
{
if(str_username==strvalue1&&str_password==strvalue2)
{
AfxMessageBox("用户登录成功!");
return;
}
else
{
// remember++;
//return;
}
}
adoconnect.m_pRecordset->MoveNext();
}
AfxMessageBox("对不起,您输入的用户名或密码不正确,请重新输入!");
}
相关文章推荐
- Qt5.9Creator登录界面函数总结(通过连接远程服务器数据库MySql5.7.17进行登录用户名和密码验证)
- QuickCSharp框架开发(18)------加密用户名和密码数据与数据库读出的密码进行哈希比较(验证用户是否成功登录)
- easyUI(四) -- SpringMVC+MyBatis怎么验证用户名和密码进行登录
- 利用SQLite数据库进行用户名、密码的注册和登录验证
- 1 完成登录页面(到记事本文件进行密码和用户名的验证)。 2 完成 用户注册页面(将注册信息显示在本页面 div 中)。
- 单点登录之cas4.2.7服务端数据库配置(读数据库验证用户并对密码进行加密)
- 登录判断用户名和密码是否正确的代码(链接和读取数据库)
- 在安装discuz!nt时,数据库用户登录失败的问题时,sql server 2008 数据库更改登录模式(sql server 2008 混合登陆中SqlServer身份验证用户名密码)
- SSO单点登录 cas_server通过查询数据库验证用户名、密码正确性
- PHP登录对用户名、密码进行验证及登录状态操作
- php登录验证(包括用户名、密码、验证码、数据库、是否已登陆验证、自动登录和注销登录)
- 单点登录之cas4.2.7服务端数据库配置(读数据库验证用户并对密码进行加密)
- 怎样在VC中用代码操作SQL(1)?---注册帐号,修改密码,登录3个功能
- 通过CouchDB-Python连接带有用户名/密码验证的数据库
- Microsoft SQL Server 2000数据库添加用户名和密码
- IIS 访问需要用户名和密码 (需要进行身份验证)
- [JSP] 利用JavaScript来用户名和密码进行客户端的验证
- 根据数据库中的值进行验证登录授权
- 一段VB.NET代码,生成邮件,发送邮件,支持SMTP验证用户名密码.
- 当点击登录的时候,弹出一个对话框,让你写用户名和密码