如何解决如何在C ++ Windows窗体应用程序中全局保存用户数据
这里是帮助登录用户的按钮处理程序,我想将用户数据检索到整个程序都使用的全局数据结构中,这与PHP Web Session有关。如何使用下面的代码实现和检索
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MysqL::Data::MysqLClient;
private: System::Void loginBtn_Click(System::Object^ sender,System::EventArgs^ e) {
String^ email = this->email->Text;
String^ password = this->password->Text;
if (email == " " || password == "") {
MessageBox::Show("Please enter all fields to proceed");
}
else {
String^ connString = L"datasource=127.0.0.1;port=3306;username=root;password=";
MysqLConnection^ connDb = gcnew MysqLConnection(connString);
MysqLCommand^ cmdDb = gcnew MysqLCommand("select * from clocking.users where EMAIL = '" + email + "' AND PASSWORD = '" + password + "' ",connDb);
MysqLDataReader^ myReader;
try {
connDb->open();
myReader = cmdDb->ExecuteReader();
int row = 0;
while (myReader->Read()) {
row = row + 1;
}
if(row == 1){
MessageBox::Show("Welcome on board! Auth Successful");
}
else {
MessageBox::Show("Incorrect Email/Password Combination. Try again!");
}
}
catch (Exception^ex) {
MessageBox::Show("Error Connecting to System Database!");
}
}
}
解决方法
Hello Lewa Bammy Stephen,您的代码很好,您只需添加一个类,即可在其中使用适当的get和set方法保存用户数据,
public bool Login(string Nombre,string Contraseña)
{
using (var conection = Getconection())
{
conection.Open();
using (var command = GetSqlCommand())
{
command.Connection = conection;
command.CommandText = "select * from Usuario where Usuario = @Usuario and claveusu =@claveusu";
command.Parameters.AddWithValue("@Usuario",Nombre);
command.Parameters.AddWithValue("@claveusu",Contraseña);
command.CommandType = CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Datoscahe.Iduser = reader.GetInt32(0);
Datoscahe.Name = reader.GetString(1);
Obtenerrol();
}
return true;
}
else
return false;
}
}
}
如果您仔细查看while条件,我会告诉它调用Data类cahe,以便它为我保存启动会话的人员的信息,该信息指示其值的类型以及它在数据库中的位置
namespace Capadatos.SQLserver
{
public static class Datoscahe
{
public static int Idusuario { get; set; }
public static string Nombre { get; set; }
public static string Apellidos { get; set; }
public static string Sexo { get; set; }
public static DateTime Fecha_nacimiento { get; set; }
public static string Num_documento { get; set; }
public static string Direccion { get; set; }
public static string Telefono { get; set; }
public static string Email { get; set; }
public static int Idrol { get; set; }
public static string Usuario { get; set; }
public static string Password { get; set; }
}
}
这是我保留用户数据的类
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。