Oracle插入查询

时间:2014-06-06 18:37:40

标签: c# oracle10g

我收到此错误“ORA-01722:非法/无效号码”

            decimal first2var = Convert.ToDecimal(var1);
            decimal id_refeicao;

            //...
            // save values in var insted of lables
            decimal id_usuario = (decimal)dr.GetInt32(0);
            //...
            var idUsuarioParameter = new OracleParameter
            {
                ParameterName = "id_usuario",
                OracleDbType = OracleDbType.Decimal,
                Direction = ParameterDirection.Input,
                Value = id_usuario
            };
            var idRefeicaoParameter = new OracleParameter
            {
                ParameterName = "id_refeicao",
                OracleDbType = OracleDbType.Decimal,
                Direction = ParameterDirection.Input,
                Value = id_refeicao
            };
            var var1Parameter = new OracleParameter
            {
                ParameterName = "first2var",
                OracleDbType = OracleDbType.Decimal, //-- populate with correct oracle type
                Direction = ParameterDirection.Input,
                Value = first2var
            };
                OracleConnection connection = new OracleConnection(oradb);
                connection.Open();
                cmd.Connection = connection;
                cmd.CommandText = "INSERT INTO SER_REFEICAO_USUARIO (USUARIO, REFEICAO, DATA_HORA, ORIGEM ) VALUES(:id_usuario, :id_refeicao, SYSDATE , :first2var )";
                cmd.Parameters.Add(idUsuarioParameter);
                cmd.Parameters.Add(idRefeicaoParameter);
                cmd.Parameters.Add(var1Parameter);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                connection.Close();

任何人都可以帮助我,我在这里做错了,如果我在我的插入查询中硬编码我的值,它工作正常,但如果我传递上述参数,所以我得到错误。

2 个答案:

答案 0 :(得分:1)

根据此source,您应该从参数名称中省略:,因此它应该是这样的:

cmd.Parameters.Add("id_usuario", id_usuario);
cmd.Parameters.Add("id_refeicao", id_refeicao);
cmd.Parameters.Add("var1", var1);

此外,正如评论中所述,您的查询文字不匹配:

INSERT INTO SER_REFEICAO_USUARIO (USUARIO, REFEICAO,    DATA_HORA,  ORIGEM)
 VALUES(:id_usuario, :id_refeicao, SYSDATE, :var2 )

您的参数声明:

cmd.Parameters.Add("var1", var1);

答案 1 :(得分:0)

我假设你正在使用ODP.NET。根据我的经验,我总是必须指定OracleDbType.Decimal,因为由于某种原因,Oracle不喜欢byte,int或long。另一个问题是我们没有传递OracleDbType和参数,所以ODP.NET看到你传入一个字符串,并且可能说参数是string类型而不是你想要的类型。再加上Sven Grosen的回答应该为你做。你得到的结果如下:

decimal id_usuario = (decimal)dr.GetInt32(0);
decimal id_refeicao = (decimal)dtre.GetInt32(0);
var idUsuarioParameter = new OracleParameter
{
    ParameterName = "id_usuario",
    OracelDbType = OracleDbType.Decimal,
    Direction = ParameterDirection.Input,
    Value = id_usuario
};
var idRefeicaoParameter = new OracleParameter
{
    ParameterName = "id_refeicao",
    OracelDbType = OracleDbType.Decimal,
    Direction = ParameterDirection.Input,
    Value = id_refeicao 
};    
var var1Parameter = new OracleParameter
{
    ParameterName = "var1",
    //OracelDbType = OracleDbType.Decimal, -- populate with correct oracle type
    Direction = ParameterDirection.Input,
    Value = var1 
};


connection.Open();
cmd.Connection = connection;
cmd.CommandText = "INSERT INTO SER_REFEICAO_USUARIO (USUARIO, REFEICAO,    DATA_HORA,  ORIGEM) VALUES(:id_usuario, :id_refeicao, SYSDATE, :var1 )";
cmd.Parameters.Add(idUsuarioParameter);
cmd.Parameters.Add(idRefeicaoParameter);
cmd.Parameters.Add(var1Parameter);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();

我认为应该为你做。注意:未经测试/未编译的代码。