2011-12-15 16 views
0

一般的なサーバに保持されているテーブルから取得した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() 

申し訳ありませんがコードがたくさんありますが、それはかなり簡単で、私はそれが問題に役立つだろうと思った。

これが機能しなくなることは明らかですか?

+0

'sqlQuery'の値を投稿してください。 – Oded

+0

また、Oracleプロバイダの代わりにOleDbを使用しているのは何ですか? – Oded

+0

Oracleデータベースを使用しているだけではありません。私たちは約6種類のデータベースを持ち、OLEは私たちのニーズに合っています。コマンドは異なるデータベース方言ごとに格納されており、必要なものを取得し、実行する前にいくつかのパラメータにパラメータを追加する必要があります。私は単純なString.Replace( "{0}"、itemId)を使いたいと思っていました。上司は私に、上記のようなものを使用することを望んでいます。代わりに、それが行われる通常の方法です。エラー・メッセージには、sqlQueryの値が表示されます。 – CSharpened

答えて

2

問題は、OleDBプロバイダがクエリで名前付きパラメータをサポートしていないことです。

この:

select VALUE from RWOL_CONFIGURATION where ID = @ItemId 

は次のようになります。

select VALUE from RWOL_CONFIGURATION where ID = ? 

は例のために、MSDNでOleDbParameterを参照してください。

+0

ありがとうございます。複数のパラメータを使用できるように実装を変更できますか?私はちょうど使用している場合?私が問題を抱えていると仮定して、どこに2つの句を入れて、それを使用しようとしていますか?両方のための。申し訳ありませんが、MSDNではこれの例は示していません。私はm_oleDatabaseCommand.Parameters.AddWithValue(m_parameterName [i]、m_parameterValue [i])を使用します。コマンドオブジェクトにパラメータを追加します。 – CSharpened

+1

@CSharpened - '? 'をさらに追加できます。パラメータごとに1つあり、パラメータの順番に置換されます(最初は最初のパラメータなど...)。これは、_same_パラメータを何回か使用する必要がある場合は、それを何度も追加する必要があることを意味します。 – Oded

+0

大丈夫です。ご協力いただきありがとうございます。 – CSharpened