使用 WriteToServer 方法日期时间列插入数据会出现异常
insert data using WriteToServer method datetime column gives exception
我正在尝试将 DateTime
插入到数据类型为日期时间的列中。该值是使用 DateTime.Now
分配的,因此我知道代码中不存在类型不正确的问题。
我得到的异常是:
The given value of type String from the data source cannot be converted
to type datetime of the specified target column.
这就是我指定数据表列的方式:
DataTable HODetails = new DataTable();
HODetails.Columns.Add("MasterID", typeof(long));
HODetails.Columns.Add("ItemID", typeof(int));
HODetails.Columns.Add("SubCategoryID", typeof(int));
HODetails.Columns.Add("BatchNo", typeof(string));
HODetails.Columns.Add("ExpiryDate", typeof(DateTime));
这是我设置数据的方式:
HODetails.Rows[HODetails.Rows.Count - 1]["ItemID"] = Convert.ToInt32(lblItemId.Text);
HODetails.Rows[HODetails.Rows.Count - 1]["MasterID"] =HOReceipt_ID;
HODetails.Rows[HODetails.Rows.Count - 1]["SubCategoryID"] = SubCatID;
HODetails.Rows[HODetails.Rows.Count - 1]["BatchNo"] = Convert.ToString(txtBatchNo.Text == "" ? "" : txtBatchNo.Text);
HODetails.Rows[HODetails.Rows.Count - 1]["ExpiryDate"] = DateTime.Now;
那么为什么 WriteToServer()
认为 "value of type String from the data source cannot be converted to type datetime"?
我的问题解决了
我更改了我的 writeToServer 方法
if (DtWithTableName.Rows.Count > 0)
{
using (SqlBulkCopy s = new SqlBulkCopy(strConnection))
{
try
{
s.DestinationTableName = "HOMaterialReceiptDetails";
s.ColumnMappings.Add("MasterID", "MasterID");
s.ColumnMappings.Add("ItemID", "ItemID");
s.ColumnMappings.Add("SubCategoryID", "SubCategoryID");
s.ColumnMappings.Add("ExpiryDate", "ExpiryDate");
s.ColumnMappings.Add("BrandName", "BrandName");
s.ColumnMappings.Add("Qty", "Qty");
s.ColumnMappings.Add("FreeQty", "FreeQty");
s.ColumnMappings.Add("ReturnQty", "ReturnQty");
s.ColumnMappings.Add("ReplacementQty", "ReplacementQty");
s.WriteToServer(DtWithTableName);
}
catch (Exception)
{
IsInserted = false;
}
}
}
我正在尝试将 DateTime
插入到数据类型为日期时间的列中。该值是使用 DateTime.Now
分配的,因此我知道代码中不存在类型不正确的问题。
我得到的异常是:
The given value of type String from the data source cannot be converted
to type datetime of the specified target column.
这就是我指定数据表列的方式:
DataTable HODetails = new DataTable();
HODetails.Columns.Add("MasterID", typeof(long));
HODetails.Columns.Add("ItemID", typeof(int));
HODetails.Columns.Add("SubCategoryID", typeof(int));
HODetails.Columns.Add("BatchNo", typeof(string));
HODetails.Columns.Add("ExpiryDate", typeof(DateTime));
这是我设置数据的方式:
HODetails.Rows[HODetails.Rows.Count - 1]["ItemID"] = Convert.ToInt32(lblItemId.Text);
HODetails.Rows[HODetails.Rows.Count - 1]["MasterID"] =HOReceipt_ID;
HODetails.Rows[HODetails.Rows.Count - 1]["SubCategoryID"] = SubCatID;
HODetails.Rows[HODetails.Rows.Count - 1]["BatchNo"] = Convert.ToString(txtBatchNo.Text == "" ? "" : txtBatchNo.Text);
HODetails.Rows[HODetails.Rows.Count - 1]["ExpiryDate"] = DateTime.Now;
那么为什么 WriteToServer()
认为 "value of type String from the data source cannot be converted to type datetime"?
我的问题解决了 我更改了我的 writeToServer 方法
if (DtWithTableName.Rows.Count > 0)
{
using (SqlBulkCopy s = new SqlBulkCopy(strConnection))
{
try
{
s.DestinationTableName = "HOMaterialReceiptDetails";
s.ColumnMappings.Add("MasterID", "MasterID");
s.ColumnMappings.Add("ItemID", "ItemID");
s.ColumnMappings.Add("SubCategoryID", "SubCategoryID");
s.ColumnMappings.Add("ExpiryDate", "ExpiryDate");
s.ColumnMappings.Add("BrandName", "BrandName");
s.ColumnMappings.Add("Qty", "Qty");
s.ColumnMappings.Add("FreeQty", "FreeQty");
s.ColumnMappings.Add("ReturnQty", "ReturnQty");
s.ColumnMappings.Add("ReplacementQty", "ReplacementQty");
s.WriteToServer(DtWithTableName);
}
catch (Exception)
{
IsInserted = false;
}
}
}