0

我正在尝试为 制作通用更新功能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;
    }
}
4

2 回答 2

0

通常 SQL 认为不在引号 '' 内的任何内容都是数字,通常您将日期字符串传递给 SQL 以比较/设置日期/时间。尝试围绕您的日期/时间值进行报价

于 2012-07-13T13:34:11.263 回答
0

Ms 有一种处理空值的特殊方式,所以我们也需要为它们添加参数。此代码将为 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));
        }
    }
于 2012-07-19T11:15:14.673 回答