Receiving database search with SQL Reader

Asked

Viewed 84 times

1

Facebook is a class where I store user information, but I don’t know how I use the ExecuteReader to get the information.

Can anyone help me with this? (I’m new to the language c#)

public Usuario buscarUsuario(int id){
    Usuario usuario = new Usuario ();
    string sql = "SELECT * FROM usuario WHERE id = " + id;
    try{
        Csql = new ConectSQL();
        MySqlCommand cmd;
        cmd = new MySqlCommand();
        cmd.Connection = Csql.OpenBanco();
        cmd.CommandText = sql;
        cmd.ExecuteNonQuery();

        MySqlDataReader rdr;
        rdr = cmd.ExecuteReader();
        if(rdr.HasRows)
            while(rdr.Read()){
                usuario.Id = rdr.GetInt32(0);
                usuario.Nick = rdr.GetString(1);
                usuario.Cadastro = rdr.GetString(2);
                usuario.Tipo = rdr.GetInt32(3);
                usuario.Facebook = rdr.GetString(4); //eu não set q get usar.
            }
        Csql.CloseBanco();
        return usuario;
    }

this is the error that is appearing.

Assets/DAO/Usuariodao.Cs(52,49): error CS0029: Cannot implicitly Convert type `string' to `Facebook'

  • Facebook is a class, right? You have to do another select to get the information from it. By the way, what is the content of this column of index 4?

  • Can you post your User and Facebook class, ? and how your Facebook class data is stored in the database?

  • Post the Facebook class too so we can see!

1 answer

0

facebook class:

using UnityEngine;
using System.Collections;

public class FaceBook : MonoBehaviour {
    private int id;
    private string first;
    private string last;
    private string email;
    private byte[] foto;
    private string page_id;
    private string aniversario;

    public FaceBook(){

    }

    public FaceBook(int id, string fisrt, string last, string email, byte[] foto, string page_id, string aniversario){
        this.id = id;
        this.first = first;
        this.last = last;
        this.email = email;
        this.foto = foto;
        this.page_id = page_id;
        this.aniversario = aniversario;
    }

    public int Id{
        get{
            return id;
        }
        set{
            id = value;
        }
    }

    public string First {
        get {
            return first;
        }
        set {
            first = value;
        }
    }

    public string Last {
        get {
            return last;
        }
        set {
            last = value;
        }
    }

    public string Email {
        get {
            return email;
        }
        set {
            email = value;
        }
    }

    public byte[] Foto{
        get{
            return foto;
        }
        set{
            foto = value;
        }
    }

    public string Page_id{
        get{
            return page_id;
        }
        set{
            page_id = value;
        }
    }

    public string Aniversario{
        get{
            return aniversario;
        }
        set{
            aniversario = value;
        }
    }
}

Browser other questions tagged

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