我正在嘗試使用「%」來搜索日期範圍。在表中,日期格式爲「2015年1月1日」,我試圖返回特定月份的所有日期(例如2015年3月)的數據。c#sql通配符不起作用
我創建的格式的兩個字符串...
String datePrev1 = monthLast + "%";
String datePrev2 = "%" + yearLast;
凡datePrev1 == 「03%」 和datePrev2 == 「2015年%」,我都已經確認將正確生成。
我的SQL搜索方法是...
SqlCommand selectCommand = new SqlCommand("SELECT project, prevWeekHours FROM submissionsHours WHERE (WWID = '" + ID + "' AND (datePrevWeek LIKE '" + datePrev1 + "' AND datePrevWeek LIKE '" + datePrev2 + "'))", Conn1);
Conn1.Open();
SqlDataReader reader = selectCommand.ExecuteReader();
int totalTime = 0;
while (reader.Read() == true)
{
project.Add(reader[0].ToString());
projTime.Add(Convert.ToInt32(reader[1]));
totalTime += Convert.ToInt32(reader[1]);
}
Conn1.Close();
int index = project.Count; //total amount of projects
testLabel.Text = index.ToString() + "..." + datePrev1+datePrev2;
的testLabel.Text我爲故障指示沒有這個字符串匹配時在表中肯定條目索引返回0,那應該匹配它。
關於我在做什麼的錯誤?
我試圖查詢像...
... WHERE datePrevWeek LIKE '03/__/2015';
... WHERE datePrevWeek LIKE '03____2015';
要沒有成功...謝謝。
不要商店的日期作爲Varchar將它們存儲爲DateTime的 – Magnus 2015-03-13 12:58:21
您是否嘗試過只運行預期的SQL查詢以確保返回結果?當你運行它來檢查你的SQL查詢的輸出時,你是否也嘗試過分析它? – 2015-03-13 12:58:25
使用SQL參數暴露給SQL注入的參數。 – Lloyd 2015-03-13 13:00:06