获取值而不是整个查询
问题描述:
我想将加密的密码的值转换为字符串变量。但我正在获取整个查询。获取值而不是整个查询
这里是我的代码: -
string strpassword = "select sys.get_enc_val ('" + txtpassword.Text + "', 'F20FA982B4C2C675') from dual";
Response.Write(strpassword);
在strpassword
我得到了整个查询。
但Toad
结果是
F52377D5FFB1A47F
如何获取在Oracle?
答
当编写
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: - 更新了我的答案。请检查 –
** [可能的SQL注入(https://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx)** – lad2025
@ lad2025:我意识到这一点,但如何获得价值。任何想法 ? – BNN
'strpassword'只是一个字符串。您需要打开连接到数据库,执行SQL,并读取返回的值 – lad2025