Data Import

Asked

Viewed 92 times

4

Hello, I would like to know how I take a value from my database and take it to a field "automatically". For example, in the image below, I want this field marked to be information that is already saved in my database. I’d like to know how I do to search it and put it in this field, if that’s possible. I’m using visual studio(c#) and sql Developer(Oracle).

inserir a descrição da imagem aqui

In class I put this:

public OracleDataReader ListarUs()
{
    string strQuery = "SELECT MAX(COD_FUNCIONARIO) COD_FUNCIONARIO FROM FUNCIONARIO WHERE USUARIO='"+ _usuario+"'";
    clnBancoDados ObjBancoDados = new clnBancoDados();
    return ObjBancoDados.RetornaDataReader(strQuery);
}

but nothing happens.

  • 3

    In the onLoad form you select in the bank and put the amount you want in your TextBox.

  • You want when you press "save" it to show the generated code to the employee at the base, this?

  • You want me to show you the next ID that belongs to this new employee, right? For example, the last employee have the ID 23, the next registration would have the ID 24, then present the Employee Code 24 direct in this TextBox. Would that be?

  • I actually already have that ID. I have a login screen, and I wanted this field to be the employee ID (which is already registered and has an ID) that is logged in to the system.

  • Enzo, show the code of what you’ve done. Otherwise it looks like you want us to do it for you.

  • @Try to explain your problem to me better: what you need is to take the Employee ID logged in and put in the textbox? I’m sure?

  • Yes, that’s right. Since there is more than one Employee, I would have to "identify" which one of these would be logged in, to import your Id into the textbox. I want to import the Id as soon as I open the form(it already open the form with the field filled).

Show 2 more comments

2 answers

1

An example of User CRUD


Connection at the bank

  using System.Configuration;
  using MySql.Data.MySqlClient;

  namespace PrjGpaci
  {
 class sisBdConn
  {
    protected MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["PrjGpaci.Properties.Settings.gpaciConnectionString1"].ToString());

    protected bool abrir()
    {
        try
        {
            conn.Open();
            return true;
        }
        catch (MySqlException ex) { return false; }
    }

    protected bool fechar()
    {
        try
        {

            conn.Clone();
            return true;
        }
        catch (MySqlException ex) { return false; }
    }
}
}

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Data;
  using MySql.Data.MySqlClient;

  namespace PrjGpaci
  {
  class sisBdUsr:sisBdConn
  {


    /// <summary>
    /// Cadastra o usuario
    /// </summary>
    /// <param name="r"> Classe Usr</param>
    /// <returns>  verdadeiro se cadastrou ou falso se não cadastrou </returns>
    public bool cadUsr(Usr r)
    {
        try
        {

            abrir();

            string strquery = "INSERT INTO usr (id,psw,nome,tipo) VALUES (@id,@psw,@nome,@tipo)";
            MySqlCommand comand = new MySqlCommand(strquery, conn);

            comand.Parameters.AddWithValue("@id",r.Id);
            comand.Parameters.AddWithValue("@psw",r.Psw);
            comand.Parameters.AddWithValue("@nome",r.Nome);
            comand.Parameters.AddWithValue("@tipo", r.Tipo);

            comand.ExecuteNonQuery();

            fechar();
            return true;
        }
        catch (Exception)
        {

            throw;
        }
    }

    /// <summary>
    /// Atualiza as informaçoes do usuario
    /// </summary>
    /// <param name="r"></param>
    /// <returns></returns>
    public bool upUsr(Usr r)
    {
        try
        {
            abrir();

            string strquery = "UPDATE usr SET id =@id,psw =@psw,nome =@nome,tipo = @tipo WHERE codu = @codu";
            MySqlCommand comand = new MySqlCommand(strquery, conn);

            comand.Parameters.AddWithValue("@id", r.Id);
            comand.Parameters.AddWithValue("@psw", r.Psw);
            comand.Parameters.AddWithValue("@nome", r.Nome);
            comand.Parameters.AddWithValue("@tipo", r.Tipo);
            comand.Parameters.AddWithValue("@codu", r.Cod);

            comand.ExecuteNonQuery();

            fechar();                
            return true;
        }
        catch (Exception)
        {

            throw;
        }
    }

    /// <summary>
    /// Consulta para login
    /// </summary>
    /// <param name="id">id</param>
    /// <param name="psw">senha</param>
    /// <returns> retorna classe usr null se não encontrar ou reotna usr com os dados do usr</returns>  
    public Usr login(string id, string psw)
    {
        try
        {
            Usr u = null;
            abrir();
            string strquery = "SELECT * FROM usr WHERE Id LIKE @id AND Psw LIKE @psw";
            MySqlCommand comand = new MySqlCommand(strquery, conn);

            comand.Parameters.AddWithValue("@id", id);
            comand.Parameters.AddWithValue("@psw", psw);

            MySqlDataReader ler = comand.ExecuteReader();

                while (ler.Read())
                {
                    u = new Usr();
                    u.Cod = Convert.ToInt32(ler["codu"]);
                    u.Id = ler["id"].ToString();
                    u.Psw = ler["psw"].ToString();
                    u.Nome = ler["nome"].ToString();
                    u.Tipo = Convert.ToInt32(ler["tipo"]);
                    fechar();
                    return u;

                }


            fechar();
            return null;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }

    /// <summary>
    /// busca todos os usuario
    /// </summary>
    /// <returns>retorna dataset</returns>
    public DataSet dataUsr()
    {
        try
        {
            abrir();
            string strquery = "SELECT codu ,id ,psw,nome,tipo FROM usr";

            MySqlCommand comand = new MySqlCommand(strquery, conn);
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(comand);
            da.Fill(ds);
            fechar();


            return ds;
        }
        catch (Exception)
        {
            return null;
            throw;
        }

    }

    /// <summary>
    /// Pesquisa por codigo
    /// </summary>
    /// <param name="cod"></param>
    /// <returns>retorna dataset</returns>
    public DataSet dataUsr(int cod)
    {
        try
        {
            abrir();
            string strquery = "SELECT codu ,id ,psw,nome,tipo FROM usr WHERE codu = @cod";

            MySqlCommand comand = new MySqlCommand(strquery, conn);
            comand.Parameters.AddWithValue("@cod",cod);
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(comand);
            da.Fill(ds, "tbUsuario");
            fechar();


            return ds;
        }
        catch (Exception)
        {
            return null;
            throw;
        }

    }

    /// <summary>
    /// Pesquisa por id
    /// </summary>
    /// <param name="id"></param>
    /// <returns>retorna dataset</returns>
    public DataSet dataUsr(string id)
    {
        try
        {
            abrir();
            string strquery = "SELECT codu ,id ,psw,nome,tipo FROM usr WHERE id LIKE @id";

            MySqlCommand comand = new MySqlCommand(strquery, conn);
            comand.Parameters.AddWithValue("@id", id);
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(comand);
            da.Fill(ds);
            fechar();
            return ds;
        }
        catch (Exception)
        {
            return null;
            throw;
        }

    }

    /// <summary>
    /// Pesquisa por nome
    /// </summary>
    /// <param name="nome"></param>
    /// <returns></returns>
    public DataSet pesqNome(string nome)
    {
        try
        {
            abrir();
            string strquery = "SELECT codu ,id ,psw,nome,tipo FROM usr WHERE nome LIKE @nome";

            MySqlCommand comand = new MySqlCommand(strquery, conn);
            comand.Parameters.AddWithValue("@nome", nome);
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(comand);
            da.Fill(ds);
            fechar();
            return ds;
        }
        catch (Exception)
        {
            return null;
            throw;
        }

    }

    /// <summary>
    /// Pesquisa o id exato do usario e retorna verdadeiro ou falso
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public bool pesqIdExiste(string id)
    {
        try
        {
            abrir();
            string strquery = "SELECT codu ,id ,psw,nome,tipo FROM usr WHERE id = @id";

            MySqlCommand comand = new MySqlCommand(strquery, conn);
            comand.Parameters.AddWithValue("@id", id);

            MySqlDataReader ler = comand.ExecuteReader();
            while (ler.Read())
            {

                fechar();
                return true;
            }
            return false;
        }
        catch (Exception)
        {
            return false;
            throw;
        }

    }


    public bool deletaUsr(int cod)
    {
        try
        {
            abrir();
            string strquery = "DELETE FROM usr WHERE CodU = @codu";

            MySqlCommand comand = new MySqlCommand(strquery, conn);
            comand.Parameters.AddWithValue("@codu", cod);
            comand.ExecuteNonQuery();
            return true;

        }
        catch (MySqlException)
        {
            return false;
            throw;
        }

    }
}
   }

0

In your Form Class Constructor do the BD query and arrow the employee code for the field.

 //construtor do form.
public CadastroLogin(){

    SetCodigoFuncionario();
}



private SetCodigoFuncionario(){

   try{
     OracleDataReader = dr = ListarUs();
     txtCodigoFuncionario.Text = dr["COD_FUNCIONARIO"].ToString();
  }catch(Exception e){
     MessageBox.Show(e.Message);
}

}

This is considering that your query is running as expected. Note. I did not code in VS, so the syntax may not be 100%

Browser other questions tagged

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