如何在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; }
    }
}

这是我保留用户数据的类

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...