How to get Sum(value) between two dates using MySQL and CSharp?JPJose Palmieri4yAsked Mar 21, 2022, 10:55 PM1.3kLearn iOS ProgrammingI'am using the SQL bellow, MySQL and C# string str = "Select Sum(value) from where and date between (date1) and (date2)"; I need to get the result of this SQL on C#, how do I do that?2 RepliesKnow the answer? Post it — somebody with the same question will find it here.Sign in to answer this questionIt is the same account you read, post and publish with — and you will come straight back to this page.Sign inCreate an accountGajendra Jangid4y agoPosted Mar 22, 2022, 2:35 AMHi, Use below codes Sqlcreate proc usp_getTotal @date2 as date, @date2 as date as begin SELECT SUM(ISNULL(col, 0)) FROM tableName WHERE date BETWEEN @date1 AND @date2 end Muhammad Imran Ansari4y agoPosted Mar 22, 2022, 2:18 AMStep-1: Write a storeprocedure with data parameters at SQL side to calculate and get the sum of value. like:ALTER PROCEDURE sp_getSum @fromDate AS DATE, @toDate AS DATE AS BEGIN SELECT SUM(ISNULL(valueCol, 0)) FROM yourTable WHERE dateCol BETWEEN @fromDate AND @toDate END Step-2: Call this store procedure in C# to get the result. like:SqlConnection conn = new SqlConnection(); conn.ConnectionString = "YourConnectionString"; // Add your SQL Connection String here using (SqlCommand cmd = new SqlCommand("sp_getSum", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@fromDate", "2022-03-01"); cmd.Parameters.AddWithValue("@toDate", "2022-03-10"); if (conn.State != ConnectionState.Open) conn.Open(); int sumVal = Convert.ToInt32(cmd.ExecuteScalar()); }
Gajendra Jangid4y agoPosted Mar 22, 2022, 2:35 AMHi, Use below codes Sqlcreate proc usp_getTotal @date2 as date, @date2 as date as begin SELECT SUM(ISNULL(col, 0)) FROM tableName WHERE date BETWEEN @date1 AND @date2 end
Muhammad Imran Ansari4y agoPosted Mar 22, 2022, 2:18 AMStep-1: Write a storeprocedure with data parameters at SQL side to calculate and get the sum of value. like:ALTER PROCEDURE sp_getSum @fromDate AS DATE, @toDate AS DATE AS BEGIN SELECT SUM(ISNULL(valueCol, 0)) FROM yourTable WHERE dateCol BETWEEN @fromDate AND @toDate END Step-2: Call this store procedure in C# to get the result. like:SqlConnection conn = new SqlConnection(); conn.ConnectionString = "YourConnectionString"; // Add your SQL Connection String here using (SqlCommand cmd = new SqlCommand("sp_getSum", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@fromDate", "2022-03-01"); cmd.Parameters.AddWithValue("@toDate", "2022-03-10"); if (conn.State != ConnectionState.Open) conn.Open(); int sumVal = Convert.ToInt32(cmd.ExecuteScalar()); }
Gajendra JangidPosted Mar 22, 2022, 2:35 AM
Muhammad Imran AnsariPosted Mar 22, 2022, 2:18 AM