C#中的ADODB问题-怀疑问题是数据转换

时间:2020-03-30 23:59:37

标签: c# oracle

我正在查看一个非常老的Windows应用程序,并试图找出一个问题。

我得到的错误是“多步操作生成的错误。请检查每个状态值。”

以下是《守则》和其他详细信息

public static bool CopyFromType(object newValues, ref ADODB.Recordset20 returnValues)
    {

        bool flag = false;

        returnValues.AddNew();

        foreach (ADODB.InternalField aField in returnValues.Fields)
        {
            Type type = newValues.GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.Name.ToUpper() == aField.Name.ToUpper())
                {
                    if (property.GetValue(newValues, null) != null && property.GetValue(newValues, null).ToString() != "")
                    {
                        //cast old value type to new value
                        aField.Value = property.GetValue(newValues, null);

                        flag = true;
                    }
                }
                else
                {
                }
            }

        }
        returnValues.Update();

        return flag;
    }

在行aField.Value = property.GetValue(newValues,null);处发生错误。特定字段(PROJECT_ID)。数据库中的数据类型为adNumeric。我尝试在分配值时转换为Int仍然没有用。

enter image description here

任何帮助都会很棒。谢谢。

1 个答案:

答案 0 :(得分:0)

无论哪个property.GetValue(newValues,null)返回的值超出了字段定义的允许长度,和/或您需要测试并将返回值转换为int。

也不确定该标志是否编码正确,因为如果一次迭代失败但下一次迭代成功,则将标志设置为true。如果要检查是否有任何值失败,则应该在第一次失败后返回,取消插入。

下面的代码也消除了检查空字符串或零长度字符串的需要。

public static bool CopyFromType(object newValues, ref ADODB.Recordset20 returnValues)
{

    returnValues.AddNew();

    foreach (ADODB.InternalField aField in returnValues.Fields)
    {
        Type type = newValues.GetType();
        PropertyInfo[] properties = type.GetProperties();

        foreach (PropertyInfo property in properties)
        {
            if (property.Name.ToUpper() == aField.Name.ToUpper())
            {
                    //cast old value type to new value
                    int rst;
                    if (int.TryParse(property.GetValue(newValues, null), out rst))
                    { 
                        aField.Value = rst;
                        flag = true;
                    }
                    else
                    {
                        //Something wrong won't cast to int
                        //Kick out of function
                        returnValues.CancelUpdate();
                        return = false;

                    }
                }
            }
        }

    }
    returnValues.Update();

    return true;
}
相关问题