如何将空值作为日期时间传递给 sql server(DAL 层)

问题描述 投票:0回答:4

我在 sql server 中有一个列(可为空,但它是日期时间)..

如果在任何日期都没有选择用户,我需要为此字段传递空值,我在下面这样做了

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime expiryDate;
        DateTime? expDate = null;
         if (chkExpDate.Checked == true)
        {
            expiryDate = Convert.ToDateTime(txtExpiryDate.Text);
        }
        else if (expDate.HasValue)
        {
            expiryDate = expDate.Value;
        }
        else
            expiryDate = Convert.ToDateTime(DBNull.Value);
         // here I am getting error like "object cannot be cast from DBNULL or value

如果我必须克服这个问题我需要做什么..如果在任何日期都没有选择用户,我需要传递空值我该怎么做..

有人愿意帮忙吗..非常感谢

为此

的DAL
public bool ReAssignLicense(string certificateID, string serialNumber, string newEmail, string ticketID, string backupBy, string customerName, DateTime expDate)
    {
        List<SqlParameter> ParaList = new List<SqlParameter>();
        ParaList.Add(new SqlParameter("@certificate", certificateID));
        ParaList.Add(new SqlParameter("@certSN", serialNumber));
        ParaList.Add(new SqlParameter("@newemail", newEmail));
        ParaList.Add(new SqlParameter("@ticket", ticketID));
        ParaList.Add(new SqlParameter("@bkpBy", backupBy));
        ParaList.Add(new SqlParameter("@customer_name", customerName));
        ParaList.Add(new SqlParameter("@exp_date", expDate));

        return SqlHelper.ExecuteNonQuery(new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString),CommandType.StoredProcedure,"sp_Update",ParaList.ToArray()) > 0;
    }
c# sql-server datetime null nullable
4个回答
0
投票

你可以这样做

SqlParameter moFrom1Param = new SqlParameter("@MoFrom1", expairydate == null 
           ? DBNull.Value : expairydate);
            moFrom1Param.IsNullable = true;
            moFrom1Param.Direction = ParameterDirection.Input;
            moFrom1Param.SqlDbType = SqlDbType.DateTime;
            cmd.Parameters.Add(moFrom1Param);

您可以将 DatTime 变量设置为可空变量,无需执行任何操作即可处理您的问题。

因为您的 expiryDate 是可以为空的文件,您可以直接在 sql 命令或查询中传递 thsi 文件,您正在从数据库层调用。

你可以像这样改变这条线

expiryDate = DateTime.MinValue;

查看这篇文章:http://www.dotnetperls.com/datetime-minvalue

//
    // This won't compile!
    //
    // DateTime dateTime1 = null;
    //
    // This is the best way to null out the DateTime.
    //
    DateTime dateTime2 = DateTime.MinValue;

0
投票

检查这个

protected void btnSubmit_Click(object sender, EventArgs e)
{
  DateTime? expDate = null;
  if (chkExpDate.Checked == true)
  {
    expDate = Convert.ToDateTime(txtExpiryDate.Text);
  }

我在这里将您的 DateTime 变量设置为可为空。

public bool ReAssignLicense(string certificateID, string serialNumber, string newEmail, string ticketID, string backupBy, string customerName, DateTime? expDate)
{
  List<SqlParameter> ParaList = new List<SqlParameter>();
  ParaList.Add(new SqlParameter("@certificate", certificateID));
  ParaList.Add(new SqlParameter("@certSN", serialNumber));
  ParaList.Add(new SqlParameter("@newemail", newEmail));
  ParaList.Add(new SqlParameter("@ticket", ticketID));
  ParaList.Add(new SqlParameter("@bkpBy", backupBy));
  ParaList.Add(new SqlParameter("@customer_name", customerName));

  if (expDate != null)
    ParaList.Add(new SqlParameter("@exp_date", expDate));

  return SqlHelper.ExecuteNonQuery(new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString), CommandType.StoredProcedure, "sp_Update", ParaList.ToArray()) > 0;
}

0
投票

嗨,如果我正确理解你的问题,那么你可以在最后一个条件下将 expiryDate 设置为 null。

同样,您将需要更改变量声明 DateTime expiryDate 是否为与 DateTime 相同的可空类型? expDate = null;

所以你上面发布的修改后的语法如下

        DateTime? expiryDate;
        DateTime? expDate = null;
        if (chkExpDate.Checked == true)
        {
            expiryDate = Convert.ToDateTime(txtExpiryDate.Text);
        }
        else if (expDate.HasValue)
        {
            expiryDate = expDate.Value;
        }
        else

            expiryDate = null;

希望对你有用。


0
投票

SQL Server
只需在日期时间列中插入
NULL

INSERT INTO Table(name, datetimeColumn, ...)
VALUES('foo bar', NULL, ..);

或者,您可以使用 DEFAULT contains:

...
DateTimeColumn DateTime DEFAULT NULL,
...

然后你可以在

INSERT
语句中完全忽略它,它将被插入
NULL
值:

INSERT INTO Table(name, ...)
VALUES('foo bar', ..);
© www.soinside.com 2019 - 2024. All rights reserved.