如何获取SQL表中的列总和?

时间:2017-09-18 16:06:29

标签: c# sql datatable

我正在尝试在我的SQL表中添加一个页脚行,该行显示从DataTable复制后每列的总和,但是我得到的值为-1而不是Total值,如图所示在这里,然后通过将一些列除以计算的总和得到百分比,我的代码在下面。

提前致谢!

        using (SqlConnection dbConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\evxaf4\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"))
        {
            dbConnection.Open();

            // create table if not exists 
            string createTableQuery = @"Create Table [DesignQuantitiesTable] 
                    ( ElementType varchar(200) , GrossVol real, NetVol real, GrossArea real, NetArea real, percgrosvol real, percnetvol real)";
            SqlCommand command = new SqlCommand(createTableQuery, dbConnection);
            command.ExecuteNonQuery();


            // Copy the DataTable to SQL Server Table using SqlBulkCopy

            using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
            {
                s.DestinationTableName = "dbo.DesignQuantitiesTable";
                //foreach (var column in m_elementInformation.Columns)
                //    s.ColumnMappings.Add(column.ToString(), column.ToString());
                s.WriteToServer(m_elementInformation);
            }

            SqlCommand sumgvol = new SqlCommand("SELECT SUM(GrossVol) FROM DesignQuantitiesTable", dbConnection);
            double sumgrosvol = sumgvol.ExecuteNonQuery();

            SqlCommand sumnvol = new SqlCommand("SELECT SUM(NetVol) FROM  DesignQuantitiesTable", dbConnection);
            double sumnetvol = sumnvol.ExecuteNonQuery();


            SqlCommand sumgarea = new SqlCommand("SELECT SUM(GrossArea) FROM  DesignQuantitiesTable", dbConnection);
            double sumgrosarea = sumgarea.ExecuteNonQuery();

            SqlCommand sumnarea = new SqlCommand("SELECT SUM(NetArea) FROM  DesignQuantitiesTable", dbConnection);
            double sumnetarea = sumnarea.ExecuteNonQuery();


            SqlCommand sumrow = new SqlCommand("Insert into DesignQuantitiesTable ( ElementType, GrossVol, NetVol, GrossArea, NetArea) values ('"+ "Total" +"',"+ sumgrosvol.ToString() +","+ sumnetvol.ToString()+","+ sumgrosarea.ToString() +","+ sumnetarea.ToString() +")", dbConnection);
            sumrow.ExecuteNonQuery();

            SqlCommand percentgvol = new SqlCommand("UPDATE DesignQuantitiesTable SET percgrosvol=(" + "GrossVol/sumgrosvol" + ")"  , dbConnection);
            percentgvol.ExecuteNonQuery();

            SqlCommand percentnvol = new SqlCommand("UPDATE DesignQuantitiesTable SET percnetvol=(" + "NetVol/sumgrosvol" + ")", dbConnection);
            percentnvol.ExecuteNonQuery();


            dbConnection.Close();

        }

1 个答案:

答案 0 :(得分:0)

您不会通过使用ExecuteNonQuery方法获得要查询的列的总和 - 这将返回受非查询命令影响的列数 - 例如Update&插入。您的查询不会影响任何列,因此可能会返回-1。 (见ExecuteNonQuery returns -1)。

如果你想要一个单独的值,你应该使用ExecuteScalarQuery(https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx) - 这将返回一个需要转换(在此处进行转换)的对象到正确的结果类型。

试试这个:

SqlCommand sumnvol = new SqlCommand("SELECT SUM(NetVol) FROM  DesignQuantitiesTable", dbConnection);
double sumnetvol = (double)sumnvol.ExecuteScalarQuery();