2010-11-24 65 views
0

我有一個人才表,擁有一個擁有他們生日的專欄的所有用戶。指定年齡範圍內的人才最佳方式是什麼?這是我所擁有的,但似乎已經過去了幾天。有沒有更好的辦法?c#,建立sql來選擇一個年齡範圍的用戶

// BUILD SQL FROM FORM DATA 
sqlString += "SELECT * from Talent"; 

if (minAge != 0 || maxAge != 120) 
{ 
    // The age criteria has been change, filter by age. 

    // select all talents that have birthdays between the following 2 dates. 
    DateTime startDate = (DateTime.Now - new TimeSpan((maxAge * 365), 0, 0, 0)); // maxAge * 365 = totalDays 
    DateTime endDate = (DateTime.Now - new TimeSpan((minAge * 365), 0, 0, 0)); 
    sqlString += " WHERE Birthdate BETWEEN '" + startDate.ToString() + "' AND '" + endDate.ToString() + "'"; 
} 
+0

偏離主題,但...使用參數化的SQL,*不*字符串連接來構建您的查詢。 (無可否認,在這種特殊情況下,不存在SQL注入的風險,但參數化的SQL無論如何都是更清晰的。) – LukeH 2010-11-24 16:14:50

+0

您的`(minAge!= 0 || maxAge!= 120)`子句很奇怪:唯一不允許的範圍是0到120所以-1000到120是可以的; 0到999是可以的; 120到0是OK等等。 – LukeH 2010-11-24 16:19:06

+0

@LukeH。你能提供一些示例代碼嗎?請注意,我在數據庫中的年齡並不像int ...我只有生日的datetime,我用它來計算他們的年齡。 – RayLoveless 2010-11-24 16:47:59

回答

0

就個人而言,我已經使用startDate.ToString("yy-MM-dd 00:00:00.000")endDate.ToString("yy-MM-dd 23:59:59.000")作品最好的(注意,在結束日期範圍000中。出於某種原因,在我的經驗,SQL關閉(可能是由於某種舍入誤差的),當它涉及到的範圍。

順便說一句,你可以使用來自TimeSpan對象的靜態方法時間計算。例如,TimeSpan.FromDays(...)

0

的問題可能與DateTime.Now,它考慮的時間以及日期。嘗試用DateTime.Today代替它。

0

爲什麼不使用DateTime.AddYears方法。

DateTime startDate = DateTime.Now.AddYears(-maxAge); 

insated的

DateTime startDate = (DateTime.Now - new TimeSpan((maxAge * 365), 0, 0, 0)); 

的另一件事是:請不要使用字符串之間+運營商構建一個SQL查詢,使用StringBuilder代替。

1

假設你正在使用SQL Server ...

using (var connection = new SqlConnection(connString)) 
    using (var command = connection.CreateCommand()) { 

     string tsql = @" 
      select * 
       from Talent 
       where DATEDIFF(YEAR, BirthDay, GETDATE()) BETWEEN @minAge AND @maxAge"; 

     command.CommandText = tsql; 
     command.CommandType = CommandType.Text; 

     int minAge = 1; 
     int maxAge = 120; 

     SqlParameter minAgeParam = command.CreateParameter(); 
     minAgeParam.Direction = ParameterDirection.Input; 
     minAgeParam.DbType = SqlDbType.TinyInt; 
     minAgeParam.ParameterName = "@minAge"; 
     minAgeParam.Value = minAge; 

     SqlParameter maxAgeParam = command.CreateParameter(); 
     maxAgeParam.Direction = ParameterDirection.Input; 
     maxAgeParam.DbType = SqlDbType.TinyInt; 
     maxAgeParam.ParameterName = "@maxAge"; 
     maxAgeParam.Value = maxAge; 

     // Just unsure here whether I must add the parameters to the command, 
     // or if they are already part of it since I used the 
     // SqlCommand.CreateParameter() method. 
     // Been too long since I haven't done any ADO.NET 
     command.Parameters.Add(minAgeParam); 
     command.Parameters.Add(maxAgeParam); 

     connection.Open(); 

     SqlDataReader reader = null; 

     try { 
      reader = command.ExecuteReader(); 
      // Process your records here... 
     } finally { 
      connection.Close() 
      command.Dispose(); 
      connection.Dispose(); 
      if (reader != null) { 
       reader.Dispose(); 
      } 
     } 
    } 

@minAge@maxAge是你的年齡參數。

您也可以告訴DATEDIFF TSQL function考慮天數,月份,小時,分鐘,秒等差異。因此,您將不得不相應地轉換您的參數值。

0

它的價值 - 您的原始解決方案已關閉幾天,因爲它使用365而不是閏年會計。