准备声明问题

时间:2011-12-15 11:27:58

标签: c# prepared-statement ole

我正在尝试在我的代码中实现准备好的staments,作为向从任何通用服务器中保存的表中检索的sql命令添加参数的一种方式。我似乎无法做对。我收到以下错误:

ORA-00936: missing expression
ORA-00936: missing expression

Prepare Statement: select VALUE from RWOL_CONFIGURATION where ID = @ItemId

我的猜测是,它只是没有取代价值,但我不知道我错过了什么。

我正在尝试以下方法来达到预期效果。我创建我的对象,从数据库中的表中获取查询字符串,将其添加到命令,将参数添加到列表对象,然后使用下面显示的最终方法将它们绑定在一起并运行查询:

//This function gets me a config item from the database
private string GetConfigurationItem(string itemId)
    {
        //new database connection object
        OleDataBaseConnection oleDataBaseConnection = new OleDataBaseConnection();

        //todo get this query from the sql factory
        SqlFactory sqlFactory = new SqlFactory();

        //This method gets the query string from the database
        string sqlQuery = sqlFactory.GetQueryString("GET_CONFIGURATION_ITEM", m_dialect);

        if (!String.IsNullOrEmpty(sqlQuery))
        {
            //add parameter to list
            oleDataBaseConnection.AddStoredProcedureParameter("@ItemId", itemId);

            //execute the sql command after adding the parameters to the command 
            oleDataBaseConnection.OleExecutePrepareStatementWithParametersQuery(sqlQuery);

            string returnValue = oleDataBaseConnection.NextRecord() ? oleDataBaseConnection.GetFieldById(0) : "Error";

            oleDataBaseConnection.Close();

            return returnValue;
        }
        else
        {
            return "ERROR";
        }

    }

//adds the parameters to list objects ready for the next method
 public void AddParameter(string parameter, object value)
    {
        m_parameterName.Add(parameter);
        m_parameterValue.Add(value);
    } // End of void AddParameter()

    /// <summary>
    /// Executes a command with the parameters passed to AddParameter(parameterName, parameterValue) and creates a recordset.
    /// </summary>
    /// 
    /// <param name="commandName">The name of the stored procedure to execute.</param>
    public bool OleExecutePrepareStatementWithParametersQuery(string commandName)
    {
        if (String.IsNullOrEmpty(commandName))
        {
            return false;
        }

        try
        {
            PrepareConnection();

            m_oleDatabaseCommand.CommandText = commandName;
            m_oleDatabaseCommand.CommandType = CommandType.StoredProcedure;

            if (m_storedProcedureParameterName.Count != 0)
            {
                for (int i = 0; i < m_storedProcedureParameterName.Count; i++)
                {
                    m_oleDatabaseCommand.Parameters.AddWithValue(m_storedProcedureParameterName[i], m_storedProcedureParameterValue[i]);
                }

                m_storedProcedureParameterName.Clear();
                m_storedProcedureParameterValue.Clear();
            }

            m_hasRecordSet = true;

            m_oleDatabaseDataReader = m_oleDatabaseCommand.ExecuteReader();

            return true;
        }
        catch (Exception ex)
        {
            if (QueueErrors)
            {
                QueuedErrorsList.AppendLine(ex.Message);
                QueuedErrorsList.AppendLine("Prepare Statement: " + storedProcedureName);
                QueuedErrorsList.AppendLine();

                QueuedErrorCount++;

                return false;
            }

            try
            {
                Close();
            }
            catch
            {
            }

            throw new Exception(ex.Message + "\r\n\r\nPrepare Statement: " + storedProcedureName);
        }
    } // End of void OleExecutePrepareStatementWithParametersQuery()

很抱歉,如果有很多代码,但它相当简单,我认为这有助于解决问题。

有什么明显可以阻止这种情况发生吗?

1 个答案:

答案 0 :(得分:2)

问题是OleDB提供程序不支持查询中的命名参数。

此:

select VALUE from RWOL_CONFIGURATION where ID = @ItemId

应该是:

select VALUE from RWOL_CONFIGURATION where ID = ?

有关示例,请参阅MSDN上的OleDbParameter