How popular is the list of objects with select(C# and Mysql) results?

Asked

Viewed 407 times

1

string strSQL = "SELECT usuperm.idusuario, usuperm.codfunc, usuperm.perm FROM usuperm INNER JOIN usuarios ON usuarios.idusuario = usuperm.idusuario WHERE usuarios.loginusuario = @loginusuario AND usuarios.senhausuario = @senhausuario";
MySqlCommand cmdSQL = new MySqlCommand(strSQL, Conexao.Abrir());
cmdSQL.Parameters.AddWithValue("loginusuario", loginusuario);
cmdSQL.Parameters.AddWithValue("senhausuario", senhausuario);


MySqlDataReader drPerm = cmdSQL.ExecuteReader();
List<PropPerm> listPermn;


//... daqui não sei se uso foreach while ou if

2 answers

1

Oops, thanks Igor, but I could not do the same way you showed, I thought better and decided it would be better to use a Datatable, I did it this way and it’s Working:

string strSQL = "SELECT usuperm.idusuario, usuperm.codfunc, usuperm.perm FROM usuperm INNER JOIN usuarios ON usuarios.idusuario = usuperm.idusuario WHERE usuarios.loginusuario = @loginusuario AND usuarios.senhausuario = @senhausuario";
MySqlCommand cmdSQL = new MySqlCommand(strSQL, Conexao.Abrir());
cmdSQL.Parameters.AddWithValue("loginusuario", loginusuario);
cmdSQL.Parameters.AddWithValue("senhausuario", senhausuario);
MySqlDataReader drPerm = cmdSQL.ExecuteReader();

DataTable perm = new DataTable();

if (drPerm.HasRows == true)
{
    perm.Load(drPerm);

    return perm;
}
else
{
    return perm;
}

0

You can do with while:

string strSQL = "SELECT usuperm.idusuario, usuperm.codfunc, usuperm.perm FROM usuperm INNER JOIN usuarios ON usuarios.idusuario = usuperm.idusuario WHERE   usuarios.loginusuario = @loginusuario AND usuarios.senhausuario =    @senhausuario";
MySqlCommand cmdSQL = new MySqlCommand(strSQL, Conexao.Abrir());
cmdSQL.Parameters.AddWithValue("loginusuario", loginusuario);
cmdSQL.Parameters.AddWithValue("senhausuario", senhausuario);

MySqlDataReader drPerm = cmdSQL.ExecuteReader();
List<PropPerm> listPermn = new List<PropPerm>();
while(drPerm.Read())
{
    PropPerm pp = new PropPerm() {
        //suas propriedades. Ex:
        Id = Convert.ToInt32(drPerm["id"]);
    };
    listPermn.Add(pp);
}
drPerm.Close();
cmdSQL.Close();

See also

I advise you to take a look at the block using. It ensures that resources such as connection, command and reader will always be closed.

Here has a Devmedia post about it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.