只是一個快速的問題。我已經嘗試了很多可能性來獲取日期列中的空值和值,例如使用 ISNULL、NULL、0,但回傳 0 結果。我不知道如何編程以從創建日期獲取空值。
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
FillDates();
}
}
private void FillDates()
{
txtDateS.Text = DateTime.Now.ToString("01-01-yyyy");
txtDateE.Text = DateTime.Now.ToString("12-31-yyyy");
}
private string ConvertDate(Object obj)
{
if (obj is DateTime)
{
return ((DateTime)obj).ToString("dd-MM-yyyy");
}
else
{
return obj.ToString();
}
}
protected void AddRowItem(StringBuilder sb, SqlDataReader reader, int index)
{
sb.Append("<tr><td>").Append(index).Append("</td>");
sb.Append("<td>").Append(ConvertDate(reader[0])).Append("</td>");
sb.Append("</tr>");
}
protected int DoItemList(SqlConnection connection, StringBuilder sb, string DateS, string DateE, string filter)
{
string sql = "";
SqlCommand readCommand = null;
SqlDataReader reader = null;
int count = 0;
try
{
string itemFilter = DoFilter(filter);
sql = "SELECT v.[Code] FROM [CAS].[dbo].[Van] v WHERE v.[Created_DateTime]>='" DateStart "' and b.[Created_DateTime]<='" DateEnd "';
readCommand = new SqlCommand(sql, connection);
reader = readCommand.ExecuteReader();
while (reader.Read())
{
count ;
AddRowItem(sb, reader, count);
}
reader.Close();
}
catch (Exception e)
{
//
}
return count;
}
我要在文本框中鍵入的網站
<div>
<asp:Label id="Label1" runat="server" Width="100px">Date From:</asp:Label>
<asp:TextBox id="txtDateS" runat="server" Width="184px"></asp:TextBox><br />
<br/>
<asp:Label id="Label2" runat="server" Width="100px">Creation date from:</asp:Label>
<asp:TextBox id="txtDateE" runat="server" Width="184px"></asp:TextBox><br />
<asp:Button id="cmdRun" runat="server" Width="91px" Height="24px" Text="Run" onclick="cmdRun_Click"></asp:Button>
<asp:Label id="lblStatus" runat="server"></asp:Label>
<hr noshade>
</div>
結果輸出
| 代碼 | 創立日期 |
|---|---|
| A0001 | 2018-08-12 |
| A0002 | 空值 |
為了進一步澄清, Created_DateTime 可以有空值。因此,當我使用文本框按 Created_DateTime 過濾時,它只顯示 Created_DateTime 不為空的值。例如開始日期,我輸入 2021-01-30,結束日期我輸入 2021-12-30。它僅顯示具有 Created_DateTime 的行,但在有空值 Created_DateTime 時不顯示。
uj5u.com熱心網友回復:
根據您的評論,您似乎總是希望回傳帶有NULL Created_DateTime. 在這種情況下,您就是這樣做的。
SELECT v.Code
FROM dbo.Van v
WHERE (
v.Created_DateTime >= @DateStart
-- When dealing with dates that are stored as datetime and could therefore have an accidental time component
-- its safer to use less than a day in the future
AND v.Created_DateTime < DATEADD(DAY, 1, CONVERT(DATE,@DateEnd))
)
OR v.Created_DateTime IS NULL;
注意我正在向您展示您應該使用的查詢,它使用引數而不是字串連接。因為字串連接讓您對 SQL 注入開放。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415062.html
標籤:
