我正在開發一個ATM軟件,我想通過輸入開始日期和結束日期來獲取報告。日期保存在我的表格中的形式是字符串dd/MM/yyyy
。我正在嘗試下面的代碼,並得到不正確的語法異常。從數據庫獲取字符串數據並將其轉換爲查詢中的日期對象
public DataTable getReportByDate(DateTime startDate, DateTime endDate)
{
try
{
DataTable table = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * from [Transaction] Where CAST(CurrDate AS Date) >=" + startDate + " AND CAST(CurrDate AS Date) <=" + endDate + ";", connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
}
return table;
}
catch (Exception e)
{
return null;
}
}
請幫幫我。
問候
爲什麼不在數據庫中使用基於日期的類型? – 2012-01-01 21:18:45
親愛的我已經使用varchar字符串。現在,它很難改變。請幫幫我。 – Snake 2012-01-01 21:22:01
即使現在很難改變,未來也很難改變 - 並且在任何地方使用錯誤的數據類型都會很糟糕。立即停止腐蝕:修復你的模式。 – 2012-01-01 21:33:23