DateTime格式不正确/时间正在被截断

时间:2012-02-10 20:09:03

标签: sql-server-2008 ms-access

我需要帮助,因为我无法理解我在这里做错了什么。

初始设置:

MS Access database (.mdb). 
MS Office 2010 installed. 
MS SQL Server 2008

我构建了例程,用于将记录从MS SQL Server 2008同步到MS Access数据库。表格布局和字段数

时,表格是相同的

例程的一部分有一个INSERT SQL语句

和SQL语句的一部分此代码

//Query to execute
cmdToExecute.CommandText = "INSERT INTO " + tableName + 
     "(EquipmentID, TimeInspected) VALUES(" +
     "'" + this._strEquipmentId.ToString() + "'," +
     "'" + this._dtTimeInspected.Value.ToString("yyyy-MM-dd hh:mm:ss tt") + "'";

_stTimeInspected被声明为sqlDateTime

由于某些原因,我无法在MS Access数据库表中一起获取日期和时间。

当我使用像this._dtTimeInspected.Value.ToString("yyyy-MM-dd")这样的格式时,我可以获得日期,或者 this._dtTimeInspected.Value.ToString("hh:mm:ss tt")我可以得到时间

但是出于某种原因,当我使用它时,时间被截断:_dtTimeInspected.Value.ToString("yyyy-MM-dd hh:mm:ss tt")

我甚至尝试使用参数化查询

PARAMETERS [@EquipmentID] Text ( 255 ),  [@TimeInspected] DateTime;
INSERT INTO VisualInspectionHistory ( EquipmentID, , DateInspected, TimeInspected, RecordIsUpdated, DateSync, TimeSync )
SELECT [@EquipmentID] AS Expr1,  [@TimeInspected] AS Expr2

    /// <summary>
    /// Purpose: Insert new record into database table
    /// </summary>
    /// <returns></returns>
    /// <remarks></remarks>
    public override bool Insert()
    {

        OleDbCommand cmdToExecute = new OleDbCommand();
        cmdToExecute.CommandText = "VisualInspection_Insert";
        cmdToExecute.CommandType = CommandType.StoredProcedure;

        cmdToExecute.Connection = conMainConnection;

        try
        {

            cmdToExecute.Parameters.Add(new OleDbParameter("@EquipmentID", (OleDbType)OleDbType.VarChar, 25)).Value = _strEquipmentID.ToString();
            cmdToExecute.Parameters.Add(new OleDbParameter("@TimeInspected", (OleDbType)OleDbType.Date)).Value = (DateTime)(_dtTimeInspected);


            if (bMainConnectionIsCreatedLocal)
            {
                //Open connction
                conMainConnection.Open();
            }
            else
            {
                if (cpMainConnectionProvider.IsTransactionPending)
                {
                    cmdToExecute.Transaction = cpMainConnectionProvider.CurrentTransaction;
                }
            }

            //Execute query
            iRowsAffected = cmdToExecute.ExecuteNonQuery();

            return true;
        }
        catch (OleDbException ex)
        {
            //Some error occured. Bubble it to caller and encapsulate Exception object
            throw ex;
        }
        catch (Exception ex)
        {
            //Some error occured. Bubble it to caller and encapsulate Exception object
            throw ex;
        }
        finally
        {
            if (bMainConnectionIsCreatedLocal == true)
            {
                //Close connection
                conMainConnection.Close();
            }
            cmdToExecute.Dispose();


        }
    }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

插入日期和时间
    //Query to execute cmdToExecute.CommandText = "INSERT INTO " + tableName + "(EquipmentID, TimeInspected) VALUES(" + "'" + this._strEquipmentId.ToString() + "'," + "#" + this._dtTimeInspected.Value.ToString("yyyy/MM/dd hh:mm:ss tt") + "#"; 即使没有参数化程序

插入时间

`//Query to execute
 cmdToExecute.CommandText = "INSERT INTO " + tableName + 
 "(EquipmentID, TimeInspected) VALUES(" +
 "'" + this._strEquipmentId.ToString() + "'," +
 "#" + this._dtTimeInspected.Value.ToString("hh:mm:ss tt") + "#";`

感谢您的帮助