您的位置:首页 > 数据库

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("对不起,您输入的用户名或密码不正确,请重新输入!");

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐