2013-01-17 64 views
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(); 
} 
+0

首选的做法是分解和减少耦合,而不是增加复杂性。一种功能,一种责任。但更好的是下面的建议,以结合常见的然后是子类。 – dkretz

回答

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; 
    } 
} 
+0

这听起来像一个非常愚蠢的问题,但它可能参数化正在传递到另一个方法的查询?例如,我写的查询被传递给实际执行连接和执行的Query()。我在查询数据库方面仍然非常新,而且我在白天学到更多东西,但是这无法实现。那,或者我今天没有足够的咖啡。 – MyCodeSucks

+0

@TyrionLannister:我不知道你的Query方法。也许它需要带参数的重载。在我看来,将数据库功能封装在数据库类中几乎不是一个好主意。就这样,它是f.e.很难确保连接尽快关闭(就像上面的'using'语句一样)。它不会增加太多,但可以伤害。 –

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(); 
} 
1

......怎么样

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

    tempList = "SELECT timestamp FROM table1"; 

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

    var total = tempList.Sum(); 
} 

虽然“有效查询”部分需要cl arification。

+0

该查询是一个被传递的示例查询。它可以是''从table1中选择名字“'。 – MyCodeSucks

+0

好的,只是想确保你实际上使用内联SQL而不是翻译ORM或其他。在那种情况下,出于安全原因,一定要参数化D Stanley指出的参数。 –

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); 
} 

调用不同的查询现在是很容易的,所以没有点再创建一个单一的参数化方法。