如何将DateTime对象转换为String以将其放入数据库列?

时间:2014-02-24 11:15:00

标签: c# sql-server datetime

我是 C#(我来自Java)的新手,我在将 DateTime 对象字段插入数据库时​​遇到以下问题:

我正在通过以下方式创建和更新查询:

    public void update(DataModel.Vulnerability.VulnSmall v)
    {
        string whereCond = "";
        string strSQLParametri = "";
        _strSQL = "";               // The final query that will be execute

        System.Data.Common.DbCommand command;
        command = _connection.CreateCommand();

        // Update the record related to the Id field of the passed VulnSmall object:
        if (v.Id != null)
        {
            // Update the VulnerabilityAlertDocument table
            _strSQL = "UPDATE VulnerabilityAlertDocument ";    
            whereCond = "WHERE Id = @ID ";          // Selecting the record by the Id column value
            addParameter(command, "@ID ", v.Id);
        }

        // Update VulnerabilityAlertId field of the selected record:
        if (v.VulnerabilityAlertId != null)
        {
            _strSQL += "SET VulnerabilityAlertId = @VULNERABILITYALLERTID ,";
            addParameter(command, "@VULNERABILITYALLERTID", v.VulnerabilityAlertId);
        }

        .....................................
        .....................................
        .....................................

      // Update Published field of the selected record:
      if (v.Published != null)
      {
          _strSQL += "Published = @PUBLISHED ,";
          addParameter(command, "@PUBLISHED", v.Published);
      }
        .....................................
        .....................................
        .....................................

        // Update Language field of the selected record:
        if (v.Language != null)
        {
            _strSQL += "Language = @LANGUAGE ";
            addParameter(command, "@LANGUAGE", v.Language);
        }


        _strSQL += whereCond;

        command.CommandText = _strSQL;
        _executeNoQuery(command);


    }

查询工作正常(正确更新表中指定的行),直到我不尝试设置已发布列的新值(在我的 Microsof SQL Server上< / strong>将日期时间作为数据类型)

问题是 v.Published 值是 System.DateTime 对象,当我这样做时:

        if (v.Published != null)
        {
            _strSQL += "Published = @PUBLISHED ,";
            addParameter(command, "@PUBLISHED", v.Published);
        }

它出错了。我认为这可能取决于我将 DateTime 对象放入_strSQL字符串。

我该怎么做才能解决这个问题?我是否应该先将 v.Published 值转换为String,然后再将其放入 _strSQL 字符串中?我错过了什么?

TNX

安德烈

2 个答案:

答案 0 :(得分:2)

您可以在DateTime对象中添加DbCommand值,方法是将其包装到SqlParameter对象中,如下所示:

if (v.Published != null)
{
    _strSQL += "Published = @PUBLISHED ,";
    SqlParameter published = new SqlParameter("@PUBLISHED", SqlDbType.DateTime);
    published.Value = v.Published;
    addParameter(command, published); // you have to create an overload of `addParameter()` taking 2 arguments : `DbCommand` and `SqlParameter`
}

重载addParameter()

private void addParameter(DbCommand dbCommand, object value)
{
    // ...
}

答案 1 :(得分:0)

.net DateTime结构提供了多种方法将DateTime对象作为字符串输出,例如ToLongDateString()ToShortDateString()方法可用于返回默认字符串格式化,或使用任何接受参数来定义输出格式的ToString(...)方法。

请参阅:http://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

或者您可能想要将DateTime存储为DateTime类型(如果您使用的SQL数据库支持这种类型)或整数类型。