Assign database value to a variable c#

Asked

Viewed 115 times

0

I want the value I got from the openCath to be assigned to the check variable. And then to scroll a check. abertoCath is an int of my tblCath, id Box is my Primary key of the table tblCath.

private void btnAbrirCaixa_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("server=localhost;database=dbSistema;uid=root;server=localhost;pwd=");
            MySqlCommand com = new MySqlCommand();
            com.CommandType = CommandType.Text;
            com.CommandText = "select abertoCaixa from tblCaixa where idCaixa = (select max(idCaixa) from tblCaixa);";
            com.Connection = con;
            try
            {
                con.Open();

                int checagem;

//No caso seria aqui que a variavel "checagem" vai receber o valor do abertoCaixa

                con.Close();

                if (checagem == 2)
                {

                    MessageBox.Show("Paçoca");
                }


            }
            catch (Exception error)
            {
                MessageBox.Show("Ocorreu um erro:\n" + error);

            }
        }
  • recommend using the Linnum will do exactly what you want and much more efficiently

  • I’m new to programming, what property is this?

  • is not a property, it is a c# tool to help with queries. in this link I passed has the introduction of how to use it, tam well written and will help you start in the right way

  • Another thing you are using Ef core or framework?

  • What is this aberto Caixa, What type is it, what information is there? You need to give more details in order to help. This code has several problems, just solve this will not help.

  • 1

    @Not Tmilitino, he clearly is not.

  • @Maniero, I know xD guy, just wanted to see if he knows what it is and if not guide him to use, at least I think better. About your question aberto caixa is the return of query his.

  • 1

    I think it’s worse and I would advise him not to use it unless he’s sure he needs it, almost everyone because it’s fashionable and not because he needs it. I’m sure this is the return of query, is very explicit in the code, but that doesn’t mean anything to know what to do.

  • @Maniero, blz cara! valeu ai. Only one more point pq you think worse?

  • 1

    This is another subject, but it is heavy, slow, full of limitations, almost always creates leaks of abstraction, too many things to learn and have to deal with when there is something easy that works better in all aspects and when the person knows how to use it does not even take so much work (It is for those who only know how to follow cake recipe and have not learned to program, but still it is easier)

Show 5 more comments

2 answers

1

You can solve this by using the ExecuteScalar() in this way:

int checagem;
checagem = Convert.ToInt32(com.ExecuteScalar());

This function seeks only a bank value and not a collection, as is the case of DataTable.

0


I hope it helps you, but I believe that just read the record with Mysqldatareader

private void btnAbrirCaixa_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("server=localhost;database=dbSistema;uid=root;server=localhost;pwd=");
            MySqlCommand com = new MySqlCommand();
            com.CommandType = CommandType.Text;
            com.CommandText = "select abertoCaixa from tblCaixa where idCaixa = (select max(idCaixa) from tblCaixa)";
            com.Connection = con;
            try
            {
                con.Open();
                MySqlDataReader dtreader = com.ExecuteReader();//Crie um objeto do tipo reader para ler os dados do banco

                int checagem =0 ;

                    while (dtreader.Read())//Enquanto existir dados no select
                {
                    checagem = (int)dtreader["abertoCaixa"];
                }


                con.Close();

                if (checagem == 2)
                {

                    MessageBox.Show("Paçoca");
                }


            }
            catch (Exception error)
            {
                MessageBox.Show("Ocorreu um erro:\n" + error);

            }
        }
  • Thank you very much, but I was giving error in the check int; so I did int check = 0;

  • For nothing... It is true, I have already adjusted in the answer... if it helped you, mark as certain the answer. vlw

  • another question, how do I do this with Where? i want to update a column of the highest table id, but this code does not work, I have to do: update tblCath added setCath = 32.40 Where idCath = (select max(idCand) from tblCath);

  • Open another question and I’ll help you tomorrow. Just mark me in the commentary.

Browser other questions tagged

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