Amr Yousry Osary00
Amr Yousry Osary00

Reputation: 5

how to sum 2 or more Column in big database - c#

i use database MS Accsses with C# winForm. i want to sum more than 2 dolumns in database using sql query or method for example

i tried this code. it works, but it's too slow.

 OleDbCommand command = new OleDbCommand("Select *from Vol_rate  ");
        command.Connection = conn;
        OleDbDataReader dr = null;
        conn.Open();
        try
        {
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                R1 += int.Parse((dr["R1"].ToString()));
                R2 += int.Parse((dr["R2"].ToString()));
                R3 += int.Parse((dr["R3"].ToString()));
                R4 += int.Parse((dr["R4"].ToString()));
                R5 += int.Parse((dr["R5"].ToString()));
                R6 += int.Parse((dr["R6"].ToString()));
                R7 += int.Parse((dr["R7"].ToString()));
                R8 += int.Parse((dr["R8"].ToString()));
                R9 += int.Parse((dr["R9"].ToString()));
                R10 += int.Parse((dr["R10"].ToString()));
                R11 += int.Parse((dr["R11"].ToString()));
                R12 += int.Parse((dr["R12"].ToString()));
                R13 += int.Parse((dr["R13"].ToString()));
                R14 += int.Parse((dr["R14"].ToString()));
                R15 += int.Parse((dr["R15"].ToString()));

            }
        }
        catch { MetroFramework.MetroMessageBox.Show(this, "خظأ"); }
        finally
        {
            conn.Close();
        }
        rate = R1 + R2 + R3 + R4 + R5 + R6 + R7 + R8 + R9 + R10 + R11 + R12 + R13 + R14 + R15;
        fullres = (season * vol) * 75;
        res = rate * 100.0 / fullres;
        circularProgressBar2.Text = String.Format("{0:0.0}", res);
        circularProgressBar2.Value = (int)res;

    }

how to calculate columns with smart and simple code?

Upvotes: 0

Views: 137

Answers (1)

oerkelens
oerkelens

Reputation: 5161

This should do the trick by calculating everything in your database:

SELECT sum(R1 + R2 + R3 + R4 + R5 + R6 + R7 + R8 + R9 + R10 + R11 + R12 + R13 + R14 + R15) as total FROM Vol_rate

However, seeing you have 15 generically named separate columns in your database, I suspect you have much bigger problems with your data model than just this one slow query...

Upvotes: 1

Related Questions