我需要帮助,因为我无法理解我在这里做错了什么。
初始设置:
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();
}
}
非常感谢任何帮助。
答案 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") + "#";`
感谢您的帮助