UpdateCommand因DateTime列而失败

时间:2012-07-13 13:16:02

标签: c# oracle dataset

我正在尝试为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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

Ms有一种处理空值的特殊方法,所以我们也需要为它们添加params。 此代码将生成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));
        }
    }