ORA-01008:并非所有变量都绑定在C#中

时间:2018-01-11 06:28:26

标签: c# sql oracle

我写的代码如下:

        var query = @"SELECT t0.ACT_TYPE, t0.ST_DT,t0.AC_ST_DT  FROM ACTIVITY  t0 WHERE (t0.AC_ST_DT > :p0 OR t0.ST_DT > :p0) AND t0.ACT_TYPE = :p1";
        OracleConnection connection = new OracleConnection(cs);
        try
        {              
            connection.Open();
            OracleCommand cmd = new OracleCommand(query, connection);
            OracleDataAdapter oda = new OracleDataAdapter(cmd);
            OracleParameter pp = new OracleParameter();

            DateTime date = new DateTime(2016, 01, 01);
            OracleParameter dp = new OracleParameter();
            dp.OracleDbType = OracleDbType.Date;
            dp.ParameterName = "p0";
            dp.Value = date;
            cmd.Parameters.Add(dp);

            var sp = new OracleParameter();
            sp.OracleDbType = OracleDbType.Varchar2;
            sp.ParameterName = "p1";
            sp.Value = "SC";
            cmd.Parameters.Add(sp);
            var dt = new DataTable();
            oda.Fill(dt);
            cmd.Dispose();
        }
        finally
        {
            connection.Close();
        }

我得到的错误就像 ORA-01008:并非所有变量都绑定

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

我认为不可能以这种方式重用位置参数。而是使用三个真正的参数然后绑定它们:

var query = @"SELECT t0.ACT_TYPE, t0.ST_DT,t0.AC_ST_DT  FROM ACTIVITY  t0 WHERE (t0.AC_ST_DT > :p0 OR t0.ST_DT > :p1) AND t0.ACT_TYPE = :p2";
OracleConnection connection = new OracleConnection(cs);
try
{              
    connection.Open();
    OracleCommand cmd = new OracleCommand(query, connection);
    OracleDataAdapter oda = new OracleDataAdapter(cmd);
    OracleParameter pp = new OracleParameter();

    DateTime date = new DateTime(2016, 01, 01);

    OracleParameter dp1 = new OracleParameter();
    dp1.OracleDbType = OracleDbType.Date;
    dp1.ParameterName = "p0";
    dp1.Value = date;
    cmd.Parameters.Add(dp1);

    OracleParameter dp2 = new OracleParameter();
    dp2.OracleDbType = OracleDbType.Date;
    dp2.ParameterName = "p1";
    dp2.Value = date;
    cmd.Parameters.Add(dp2);

    var sp = new OracleParameter();
    sp.OracleDbType = OracleDbType.Varchar2;
    sp.ParameterName = "p2";
    sp.Value = "SC";
    cmd.Parameters.Add(sp);

    var dt = new DataTable();
    oda.Fill(dt);
    cmd.Dispose();
}
finally
{
    connection.Close();
}