1

ASP.net で OleDbCommand を使用し、C# コードビハインドで SQL クエリの結果を読み込もうとしています。Oracle SQL Developer内で有効で適切に機能する次のOracle SQLステートメントを使用しています。

WITH "bis" AS
    (SELECT v.name AS "vendor", 
    SUM(CASE WHEN i.bi_rating=5 THEN 1 ELSE 0 END) AS "bi5", 
    SUM(CASE WHEN i.bi_rating=6 THEN 1 ELSE 0 END) AS "bi6", 
    SUM(CASE WHEN i.bi_rating=7 THEN 1 ELSE 0 END) AS "bi7" 
    FROM incident_tbl i, vendor_tbl v 
    WHERE i.vendor_code=v.vendor_code 
    GROUP BY v.name 
    ORDER BY v.name) 
SELECT DISTINCT 
    "bis"."vendor", 
    "bis"."bi5", 
    "bis"."bi6", 
    "bis"."bi7", 
    ("bis"."bi5"+"bis"."bi6"+"bis"."bi7") AS "total" 
FROM "bis" 
WHERE "vendor"!='[No Vendor]' 
ORDER BY "total" DESC

これにより、92 行の正しい結果が生成されます。ただし、ASP.net ページで実行すると、0 行が返されます。クエリをテスト メソッドに分離して、並べ替えを試みました: (conは既に存在する動作中の接続文字列です)

protected void test()
    {
        OleDbCommand sql = new OleDbCommand("WITH \"bis\" AS (SELECT v.name AS \"vendor\", SUM(CASE WHEN i.bi_rating=5 THEN 1 ELSE 0 END) AS \"bi5\", SUM(CASE WHEN i.bi_rating=6 THEN 1 ELSE 0 END) AS \"bi6\", SUM(CASE WHEN i.bi_rating=7 THEN 1 ELSE 0 END) AS \"bi7\" FROM incident_tbl i, vendor_tbl v WHERE i.vendor_code=v.vendor_code GROUP BY v.name ORDER BY v.name) SELECT DISTINCT \"bis\".\"vendor\", \"bis\".\"bi5\", \"bis\".\"bi6\", \"bis\".\"bi7\", (\"bis\".\"bi5\"+\"bis\".\"bi6\"+\"bis\".\"bi7\") AS \"total\" FROM incident_tbl i, \"bis\" WHERE \"vendor\"!='[No Vendor]' ORDER BY \"total\" DESC", con);
        try
        {
            if (con.State == ConnectionState.Closed)
                con.Open();

            OleDbDataReader reader = sql.ExecuteReader();

            while (reader.Read())
            {
                // Show on a temporary label for testing
                lblDebug.Text += "<br />New row... vendor: " + reader["vendor"].ToString();
            }
        }
        catch (Exception ex)
        {
            lblDebug.Text += "<br />ERROR WITH TEST METHOD: " + ex;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
                con.Close();
        }
    }

問題は、例外がスローされず、結果がゼロになることです。何か案は?

4

1 に答える 1

2

試してみてください

SELECT DISTINCT  
  vendor,  
  bi5,  
  bi6,  
  bi7,  
  (bi5+bi6+bi7) total  
FROM  
  (SELECT v.name vendor,  
   SUM(CASE WHEN i.bi_rating=5 THEN 1 ELSE 0 END) bi5,  
   SUM(CASE WHEN i.bi_rating=6 THEN 1 ELSE 0 END) bi6,  
   SUM(CASE WHEN i.bi_rating=7 THEN 1 ELSE 0 END) bi7  
   FROM incident_tbl i, vendor_tbl v  
   WHERE i.vendor_code=v.vendor_code  
   GROUP BY v.name  
   ORDER BY v.name) bis
WHERE vendor!='[No Vendor]'  
ORDER BY total DESC 
于 2012-09-19T14:01:55.383 に答える