2016-01-13 106 views
-2

我想將加密的密碼的值轉換爲字符串變量。但我正在獲取整個查詢。獲取值而不是整個查詢

這裏是我的代碼: -

string strpassword = "select sys.get_enc_val ('" + txtpassword.Text + "', 'F20FA982B4C2C675') from dual"; 
    Response.Write(strpassword); 

strpassword我得到了整個查詢。

Toad結果是

F52377D5FFB1A47F

如何獲取在Oracle?

+4

** [可能的SQL注入(https://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx)** – lad2025

+0

@ lad2025:我意識到這一點,但如何獲得價值。任何想法 ? – BNN

+1

'strpassword'只是一個字符串。您需要打開連接到數據庫,執行SQL,並讀取返回的值 – lad2025

回答

3

當編寫

string strpassword = "select sys.get_enc_val ('" + txtpassword.Text + "', 'F20FA982B4C2C675') from dual"; 
Response.Write(strpassword); 

然後當你不執行SQL其存在的字符串內您正在簡單地顯示的字符串值。

您正在查找的是字符串中存在的SQL的結果。要獲得存儲在字符串中的SQL結果,您需要執行它。

你可以嘗試這樣的:

string queryString = "select sys.get_enc_val ('" + txtpassword.Text + "', 'F20FA982B4C2C675') from dual"; 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     SqlCommand command = new SqlCommand(queryString, connection); 
     connection.Open(); 
     SqlDataReader reader = command.ExecuteReader(); 
     try 
     { 
      while (reader.Read()) 
      { 
       Console.WriteLine(String.Format("{0}",reader[0])); 
      } 
     } 
     finally 
     { 
      reader.Close(); 
     } 
    } 

如上評論,您的查詢是容易SQL Injection。更好的方法是使用paramterized query來擺脫它。喜歡的東西

string sql = "select sys.get_enc_val (@myvar) from dual"; 
SqlConnection connection = new SqlConnection(/* connection info */); 
SqlCommand command = new SqlCommand(sql, connection); 

command.Parameters.AddWithValue("myvar", txtpassword.Text); 
+0

由於索引超出了數組範圍而出錯。# – BNN

+0

@coder: - 更新了我的答案。請檢查 –

相關問題