[英]UpdateCommand fails with DateTime columns
I am trying to make a generic update function for OracleDataAdapter
, but it fails when I try to update the DataTable. 我正在尝试为
OracleDataAdapter
创建通用更新功能,但是当我尝试更新DataTable时失败。
The Error message is Failed to convert value from a Int32 to a DateTime
. 错误消息
Failed to convert value from a Int32 to a DateTime
。
In the DataTable the values are fully formed DataTime values, so I don't understand where the Update Commands fails... Any tips? 在DataTable中,这些值是完全形成的DataTime值,所以我不知道Update Commands在哪里失败...有什么提示吗?
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;
}
}
Generally SQL considers anything that is NOT inside quotes '' to be numeric, usually you pass date string to SQL to compare/set date/time. 通常,SQL会将引号''中没有的内容视为数字,通常将日期字符串传递给SQL以比较/设置日期/时间。 Try quotes around your value for date/time
尝试在日期/时间的值附近引用报价
Ms have a special way of handling null values, so we need to add params for them as well. Ms有一种处理空值的特殊方式,因此我们也需要为其添加参数。 This code will generate Update Commands for DataTables
此代码将为数据表生成更新命令
private void CreateUpdate()
{
//UPDATE "TABLE" SET "AD_USERID" = :AD_USERID, WHERE (("AD_USERID" = :Original_AD_USERID) AND ("MODULE" = :Original_MODULE)) AND ((:IsNull_MODIFIED_BY = 1 AND ""MODIFIED_BY"" IS NULL) OR (""MODIFIED_BY"" = :Original_MODIFIED_BY))
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());
if (GetOraType(col.DataType) == OracleType.Blob)
continue;
if (col.AllowDBNull || col.DataType == typeof(DateTime))
where += string.Format("((:IsNull_{0} = 1 AND \"{0}\" IS NULL) OR (\"{0}\" = :Original_{0})) AND ", col.ColumnName.ToUpper());
else
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)
{
ta.UpdateCommand.Parameters.Add(new OracleParameter(col.ColumnName.ToUpper(), GetOraType(col.DataType), 0, ParameterDirection.Input, col.ColumnName.ToUpper(), DataRowVersion.Current, false, null));
if (GetOraType(col.DataType) == OracleType.Blob)
continue;
if (col.AllowDBNull || col.DataType == typeof(DateTime))
ta.UpdateCommand.Parameters.Add(new OracleParameter("IsNull_" + col.ColumnName.ToUpper(), OracleType.Int32, 0, ParameterDirection.Input, col.ColumnName.ToUpper(), DataRowVersion.Original, true, null));
ta.UpdateCommand.Parameters.Add(new OracleParameter("Original_" + col.ColumnName.ToUpper(), GetOraType(col.DataType), 0, ParameterDirection.Input, col.ColumnName.ToUpper(), DataRowVersion.Original, false, null));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.