0
votes

J'ai besoin d'ajouter les variables Int dans mon code ensemble, des idées Comment?

private void button1_Click(object sender, EventArgs e)
{
    cmd = new SqlCommand("select * from package where pkgid = '" + txtpkgid.Text + "' ", con);
    con.Open();
    dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        int HC = Convert.ToInt32(dr["HC"]);
        int KC = Convert.ToInt32(dr["KC"]);
        int DC = Convert.ToInt32(dr["DC"]);
        int DONR = Convert.ToInt32(dr["DONR"]);
        int VNPR = Convert.ToInt32(dr["VNPR"]);

    }
    con.Close();


    cmd = new SqlCommand("select * from vehicle where RegistrationNo = '" + txtcarid.Text + "' ", con);
    con.Open();
    dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        int VDP = Convert.ToInt32(dr["VDP"]);
    }
    con.Close();
}
How to add the above?sorry, I'm a beginner so this probably looks really stupid to ask, any help on how to add them together is much appreciated.basically I want to get
Result = HC+KC+DC+DONR+VNPR+VDP
and display the Result in a label.The problem is I don't know how to bring Value of VDP out.

1 commentaires

Vous recherchez probablement exécutesCalar () et pas lecteur. Donc, vous pouvez faire ce Sélectionnez HC + KC + DC + DONR + VNPR + VDP du véhicule ... etc


3 Réponses :


0
votes

Quelque chose le long de ces lignes: xxx

Notez que vous devez disposer correctement des objets de commande et de lecture. Et comme l'a noté autre que l'autre commentateur, vous pouvez calculer cette somme sur le côté SQL, de sorte que vous n'avez pas à itération sur les Resultats. Si vous continuez à utiliser l'itérateur côté client, n'oubliez pas d'ajouter une manipulation nulle appropriée, si le DR [...] peut renvoyer un null ou dbnull.value .


0 commentaires

0
votes

Vous devez déclarer votre int dans la portée extérieure, ils ne sont accessibles que dans l'ensemble du bloc.

        private void button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("select * from package where pkgid = '" + txtpkgid.Text + "' ", con);
        con.Open();
        dr = cmd.ExecuteReader();

        //We declare variables here so we can access them
        int HC = 0;
        int KC = 0;
        int DC = 0;
        int DONR = 0;
        int VNPR = 0;
        int VDP = 0;

        while (dr.Read())
        {
            int HC = Convert.ToInt32(dr["HC"]);
            int KC = Convert.ToInt32(dr["KC"]);
            int DC = Convert.ToInt32(dr["DC"]);
            int DONR = Convert.ToInt32(dr["DONR"]);
            int VNPR = Convert.ToInt32(dr["VNPR"]);
        }
        con.Close();

        cmd = new SqlCommand("select * from vehicle where RegistrationNo = '" + txtcarid.Text + "' ", con);
        con.Open();
        dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            int VDP = Convert.ToInt32(dr["VDP"]);
        }

        con.Close();

        int result = HC + KC + DC + DONR + VNPR VDP;

        //We pass the total to the label
        Yourlabel.Text = result;
    }


1 commentaires

Remarque: vous pouvez utiliser la solution de polunin romain. C'est un nettoyant surtout si vous n'avez pas besoin de réutiliser ces variables.



0
votes

Je le ferais probablement comme ça, en supposant que vous analyse le sqlconnection con code> ainsi que votre enregistrementNo code> ne possède pas les mêmes colonnes que pkgid Table.

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = AddSqlResults(con).ToString();
}


0 commentaires