c#中的Oracle参数化查询

时间:2017-01-23 17:13:33

标签: c# oracle c#-4.0 ado.net sql-injection

string sqlCmd = @"SELECT r.row_id AS resp_id,
                         r.name AS resp_name
                  FROM srb.s_resp r,
                       srb.s_per_resp pr,
                       srb.s_contact c,
                       srb.s_user u
                  WHERE r.row_id = pr.resp_id
                    AND u.row_id = c.row_id
                    AND c.person_uid = pr.per_id
                    AND UPPER(u.login) = @login
                 ORDER BY r.name";

OracleConnection con = new OracleConnection(getConnectionString(username, password));
OracleCommand command = con.CreateCommand();

conSiebel.Open();
command.CommandType = CommandType.Text;
command.Connection = con;
command.CommandText = sqlCmd;

command.Parameters.Add(new OracleParameter("login", username));

IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
reader.Close();

我正在尝试将@login参数添加到上述查询中,但它没有添加,是否有人可以帮我修复此问题?

1 个答案:

答案 0 :(得分:4)

改为使用冒号(:login)。

 string sqlCmd = @"SELECT  r.row_id AS resp_id,
                                    r.name AS resp_name
                            FROM    srb.s_resp r,
                                    srb.s_per_resp pr,
                                    srb.s_contact c,
                                    srb.s_user u
                            WHERE   r.row_id = pr.resp_id
                                    AND u.row_id = c.row_id
                                    AND c.person_uid = pr.per_id
                                    AND UPPER(u.login) = :login
                                    ORDER BY r.name";