2014-10-30 59 views
1

嗨,我正在C#4.0,Visual Studio 2010中開發Windows桌面應用程序。我有以下方法從Oracle表檢索數據。ORA-00933:C#中SQL命令未正確結束錯誤#

private static MemoryStream GetStatement(OracleConnection con, int loginId, string session, string ip, string acNo, string frmDate, string toDate) 
{ 
var memoryStream = new MemoryStream(); 
using (var oraQuery =new OracleCommand(@"SELECT statement_file from 
user_account_statement where login_id=" + loginId +" and session_key='" 
+ session + "' and ipaddress='" + ip + "' and account_number='" 
+ acNo + "' and from_date=" + frmDate + " and to_date=" 
+ toDate + " and status='closed'", con)) 
{ 
using (var oraQueryResult = oraQuery.ExecuteReader()) 
if (oraQueryResult != null) 
{ 
while (oraQueryResult.Read()) 
{ 
var blob = new Byte[(oraQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))]; 
oraQueryResult.GetBytes(0, 0, blob, 0, blob.Length); 
//updated. 
memoryStream.Write(blob, 0, blob.Length); 
} 
} 
} 
return memoryStream; 
} 

在查詢時執行該方法我得到的錯誤是ORA-00933: SQL command not properly ended。 我試圖後綴;查詢,但得到相同的錯誤。 任何人都可以糾正我在哪裏出錯。

+1

壞,壞,非常壞......請使用參數來代替連接字符串。 – Reniuz 2014-10-30 08:43:39

+2

您應該始終使用[參數化查詢](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。這種字符串連接對於[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻擊是開放的。當你調試你的代碼時,你的'oraQuery'看起來像什麼?這個命令是否適用於您的數據庫管理器? (當然,我不這麼認爲) – 2014-10-30 08:44:07

+0

在命令中執行的實際格式化查詢是什麼?添加斷點,查看oraQuery,將查詢複製到oracle開發人員並嘗試執行它。 – Reniuz 2014-10-30 08:47:09

回答

0

下面的代碼爲我使用參數化查詢。謝謝大家:

private static MemoryStream GetStatement(OracleConnection con, int loginId, string session, string ip, string acNo, DateTime frmDate, DateTime toDate) 
    { 
     var memoryStream = new MemoryStream(); 
     using (
      var oraQuery = 
       new OracleCommand(
        @"SELECT statement_file from user_account_statement where login_id=:1 and session_key=:2" + 
         "and ipaddress=:3 and account_number=:4 and from_date=:5 and to_date=:6" + 
         " and status='closed'", con)) 
     { 
      oraQuery.BindByName = true; 
      oraQuery.Parameters.Add(":1", OracleDbType.Int32).Value = loginId; 
      oraQuery.Parameters.Add(":2", OracleDbType.NVarchar2).Value = session; 
      oraQuery.Parameters.Add(":3", OracleDbType.NVarchar2).Value = ip; 
      oraQuery.Parameters.Add(":4", OracleDbType.NVarchar2).Value = acNo; 
      oraQuery.Parameters.Add(":5", OracleDbType.Date).Value = frmDate; 
      oraQuery.Parameters.Add(":6", OracleDbType.Date).Value = toDate; 

      using (var oraQueryResult = oraQuery.ExecuteReader()) 
       if (oraQueryResult != null) 
       { 
        while (oraQueryResult.Read()) 
        { 
         var blob = new Byte[(oraQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))]; 
         oraQueryResult.GetBytes(0, 0, blob, 0, blob.Length); 
         memoryStream.Write(blob, 0, blob.Length); 
        } 
       } 
     } 
     return memoryStream; 
    } 
相關問題