1

我目前有两种方法:

CalculateDaily()
{
     List<string> tempList;

     // Effective query.  not what is really passed
     tempList = "SELECT timestamp FROM table1 WHERE date = today";

     var total = tempList.Sum();
}

和:

CalculateTotal()
{
     List<string> tempList;

     // Effective query.  not what is really passed
     tempList = "SELECT timestamp FROM table1"

     var total = tempList.Sum();
}

我的问题是我应该将它们分开,还是将它们组合成一个方法并运行if检查是否可行?就像是:

Calculate(bool daily)
{
     List<string> tempList;

     if(daily)
          tempList = "SELECT timestamp FROM table1 WHERE date = today";
     else
          tempList = "SELECT timestamp FROM table1";

     var total = tempList.Sum();
}
4

4 回答 4

2

我会使用一种提供开始日期和结束日期的方法。然后你可以随心所欲地使用它。

public static int Calculate(DateTime startDate, DateTime endDate)
{
    string sql = @"SELECT SUM(timestamp) 
                   FROM table1 
                   WHERE date BETWEEN @startDate AND @endDate";
    using(var con=new SqlConnection(connectionString))
    using (var cmd = new SqlCommand(sql, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@startDate", startDate);
        cmd.Parameters.AddWithValue("@endDate", endDate);
        int sum = (int)cmd.ExecuteScalar();
        return sum;
    }
}
于 2013-01-17T21:47:22.227 回答
1

我会这样做:

Calculate(bool daily)
{
     List<string> tempList;

     tempList = "SELECT timestamp FROM table1"

     if(daily)
          tempList += " WHERE date = today";

     var total = tempList.Sum();
}

或更参数化的版本(一些伪代码):

Calculate(bool daily)
{
     List<string> tempList;

     tempList = "SELECT timestamp FROM table1 WHERE (@Date IS NULL OR date = @Date)"

     if(daily)
          @Date = today;
     else
          @Date = null;

     var total = tempList.Sum();
}
于 2013-01-17T21:39:55.357 回答
1

怎么样...

Calculate(bool daily)
{
     List<string> tempList;

     tempList = "SELECT timestamp FROM table1";

     if(daily)
         tempList += " WHERE date = today";          

     var total = tempList.Sum();
}

尽管“有效查询”部分需要澄清。

于 2013-01-17T21:42:09.420 回答
1

您可以为标量查询制定通用方法

// Assumes parameter names @0, @1, @2 ... in the query.
public static T ExecuteScalar<T>(string query, params object[] parameters)
{
    using(var conn = new SqlConnection(myConnectionString))
    using (var cmd = new SqlCommand(query, conn)) {
        for (int i = 0; i < parameters.Length; i++) {
            cmd.Parameters.AddWithValue("@" + i, parameters[i]);
        }
        conn.Open();
        return (T)cmd.ExecuteScalar();
    }
}

然后为您的查询创建重载方法

public static decimal SumTable1Amount()
{
    return ExecuteScalar<decimal>("SELECT SUM(amount) FROM table1");
}

public static decimal SumTable1Amount(DateTime date)
{
    return ExecuteScalar<decimal>(
        "SELECT SUM(amount) FROM table1 WHERE date = @0",
        date);
}

public static decimal SumTable1Amount(DateTime fistDate, DateTime lastDate)
{
    return ExecuteScalar<decimal>(
        "SELECT SUM(amount) FROM table1 WHERE date BETWEEN @0 AND @1",
        fistDate, lastDate);
}

现在调用不同的查询非常容易,因此不再需要创建单个参数化方法。

于 2013-01-17T22:07:46.993 回答