首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 參數化查詢插入 NULL 值正確姿勢:DBNull.Value vs 空字串

     編輯:關於ASP.NET

使用 SqlParameter 插入可為 NULL 的欄位(DateTime、Int)時,空字串會被轉為預設值(1900-01-01、0),必須顯式指派 DBNull.Value 才能真正寫入 NULL。

錯誤示範

// 錯誤:空字串轉換為預設值
cmd.Parameters.Add("@BirthDate", SqlDbType.DateTime).Value = txtDate.Text;  // "" -> 1900-01-01
cmd.Parameters.Add("@Score", SqlDbType.Int).Value = txtScore.Text;          // "" -> 0

// 錯誤:直接指派空字串給強型別參數會拋出例外
cmd.Parameters.Add(new SqlParameter("@BirthDate", SqlDbType.DateTime));
cmd.Parameters["@BirthDate"].Value = txtDate.Text;  // FormatException

正確寫法

cmd.Parameters.Add(new SqlParameter("@BirthDate", SqlDbType.DateTime));
if (!string.IsNullOrEmpty(txtDate.Text))
{
    cmd.Parameters["@BirthDate"].Value = DateTime.Parse(txtDate.Text);
}
else
{
    cmd.Parameters["@BirthDate"].Value = DBNull.Value;  // 關鍵:明確指定 NULL
}

// 簡寫擴充方法
public static void SetValueOrNull(this SqlParameter param, string input, Func converter)
{
    param.Value = string.IsNullOrEmpty(input) ? DBNull.Value : converter(input);
}

// 使用
cmd.Parameters["@BirthDate"].SetValueOrNull(txtDate.Text, DateTime.Parse);
cmd.Parameters["@Score"].SetValueOrNull(txtScore.Text, int.Parse);

核心原則

  • DBNull.Value ≠ null:C# null 在參數指派時可能被忽略或轉為預設值,必須用 System.DBNull.Value
  • 先檢查再轉型:避免 DateTime.Parse("") 拋出 FormatException
  • 適用所有可為 NULL 型別:DateTime、Int、Decimal、Guid 等

Entity Framework / Dapper 現代做法

// EF Core:屬性型別用 DateTime?、int?,直接指派 null
entity.BirthDate = string.IsNullOrEmpty(txtDate.Text) ? (DateTime?)null : DateTime.Parse(txtDate.Text);

// Dapper:匿名物件屬性指派 null
conn.Execute("INSERT INTO T (BirthDate, Score) VALUES (@BirthDate, @Score)",
    new { BirthDate = string.IsNullOrEmpty(txtDate.Text) ? (DateTime?)null : DateTime.Parse(txtDate.Text),
          Score = string.IsNullOrEmpty(txtScore.Text) ? (int?)null : int.Parse(txtScore.Text) });
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved