在我的代碼少於,然後等於日期不起作用。基於服務的數據庫SQL Select語句的日期不起作用
在DATEBASE DUEDATE字段被選擇作爲日期類型不日期時間
sqlString = "Select [VehiNo], [DueDate], [Works] From [Daily] Where [DueDate] <= '" + today.ToShortDateString() + "' and [ServiceDate] Is NULL";
MessageBox.Show(sqlString);
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
{
cmd.CommandType = CommandType.Text;
SqlDataReader reader = cmd.ExecuteReader();
dtDue.Load(reader);
}
if (dtDue.Rows.Count > 0)
{
dataListOverDue.DataSource = dtDue;
dataListOverDue.Columns["VehiNo"].HeaderText = "Vehicle No";
}
我有3個數據網格視圖和3 SQL statment每個查找今天的到期,由於間今天和未來15天,並且已經過期。
我嘗試了很多東西,但都沒有工作,我不知道爲什麼。 我已經嘗試過這樣的參數。
string sqlString = "Select [VehiNo], [DueDate], [Works] From [Daily] Where [DueDate]='@today' and [ServiceDate] Is NULL";
cmd.Parameters.AddWithValue("@today", today.ToString());
sqlString = string.Format("Select [VehiNo], [DueDate], [Works] From [Daily] Where [DueDate] Between '@today' And '@next'");
cmd.Parameters.AddWithValue("@today", today.ToString());
cmd.Parameters.AddWithValue("@next", next.ToString());
選擇與=返回任何
與選擇之間提供了轉換失敗,< =錯誤
選擇返回在圖像顯示我的所有日期。
如果列是datetime類型,那麼爲什麼不使用參數化查詢。格式化日期時間值將不再需要。 –
@ChetanRanpariya我已經試過了,請檢查我的編輯。 –
通過下面發佈的解決方案。 –