我正在尝试为 制作通用更新功能OracleDataAdapter
,但是当我尝试更新 DataTable 时它失败了。
错误消息是Failed to convert value from a Int32 to a DateTime
。
在 DataTable 中,这些值是完全形成的 DataTime 值,所以我不明白更新命令在哪里失败......有什么提示吗?
private void CreateUpdate()
{
//UPDATE "TABLE" SET "AD_USERID" = :AD_USERID, WHERE (("AD_USERID" = :Original_AD_USERID) AND ("MODULE" = :Original_MODULE))
DataTable tbl = _DsViews.Tables[_DbName];
string value = string.Empty;
string where = string.Empty;
foreach (DataColumn col in tbl.Columns)
{
value += string.Format("\"{0}\" = :{0},", col.ColumnName.ToUpper());
where += string.Format("(\"{0}\" = :Original_{0}) AND ", col.ColumnName.ToUpper());
}
value = value.Substring(0, value.Length - 1);
where = where.Substring(0, where.Length - 5);
string sql = string.Format("UPDATE \"{0}\" SET {1} WHERE ({2})", _DbName, value, where);
ta.UpdateCommand = new OracleCommand(sql, MyDBConnection);
foreach (DataColumn col in tbl.Columns)
{
var para1 = ta.UpdateCommand.Parameters.Add(col.ColumnName.ToUpper(), GetOraType(col.DataType));
para1.SourceColumn = col.ColumnName;
para1.SourceColumnNullMapping = col.AllowDBNull;
var para2 = ta.UpdateCommand.Parameters.Add("Original_" + col.ColumnName.ToUpper(), GetOraType(col.DataType));
para2.SourceColumn = col.ColumnName;
para2.SourceVersion = DataRowVersion.Original;
para2.SourceColumnNullMapping = col.AllowDBNull;
}
}
OracleType GetOraType(System.Type type)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Boolean:
case TypeCode.Byte:
return OracleType.Byte;
case TypeCode.Char:
return OracleType.Char;
//case TypeCode.DBNull:
// return OracleType
case TypeCode.DateTime:
return OracleType.DateTime;
case TypeCode.Decimal:
case TypeCode.Double:
return OracleType.Number;
//case TypeCode.Empty:
case TypeCode.Int16:
return OracleType.Int16;
case TypeCode.Int32:
return OracleType.Int32;
case TypeCode.Int64:
return OracleType.Number;
case TypeCode.Object:
return OracleType.Blob;
case TypeCode.SByte:
return OracleType.SByte;
case TypeCode.Single:
return OracleType.Float;
case TypeCode.String:
return OracleType.NVarChar;
case TypeCode.UInt16:
return OracleType.UInt16;
case TypeCode.UInt32:
return OracleType.UInt32;
case TypeCode.UInt64:
return OracleType.Number;
default:
return OracleType.VarChar;
}
}