2016-01-13 104 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: - 更新了我的答案。请检查 –

相关问题