OracleCommand SQL 参数绑定 [英] OracleCommand SQL Parameters Binding

查看:33
本文介绍了OracleCommand SQL 参数绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绑定以下参数时遇到问题.连接有效,因为我在不使用参数的情况下对其进行了测试.但是,执行之前查询的值仍然使用@userName"而不是例如jsmith".

I have a problem with the binding of the below parameter. The connection works because I had tested it without using parameters. However, the value of the query before being executed is still using '@userName' instead of 'jsmith' for example.

有什么问题?这不是解决绑定的正确方法吗?

What is the problem? Is this not the right way to go around binding?

public static String GetFullName(String domainUser)
{
    DataTable dT;
    String fullName = "";

    OracleConnection db = DatabaseAdapter.GetConn();
    db.Open();

    OracleCommand oraCommand = new OracleCommand("SELECT fullname FROM user_profile WHERE domain_user_name = '@userName'", db);
    oraCommand.BindByName = true;
    oraCommand.Parameters.Add(new OracleParameter("@userName", domainUser));

    OracleDataReader oraReader = null;
    oraReader = oraCommand.ExecuteReader();

    if (oraReader.HasRows)
    {
        while (oraReader.Read())
        {
            fullName = oraReader.GetString(0);
        }
    }
    else
    {
        return "No Rows Found";
    }

    oraReader.Close();
    db.Close();
    db.Dispose();

    return fullName;
}

我在参数字段名称中添加了@,但它仍然没有修复它.

I added @ to the parameter field name, but it still does not fix it.

推荐答案

去掉@username,周围的单引号,对于oracle使用:和参数名代替@,比如:

Remove single quotes around @username, and with respect to oracle use : with parameter name instead of @, like:

OracleCommand oraCommand = new OracleCommand("SELECT fullname FROM sup_sys.user_profile
                           WHERE domain_user_name = :userName", db);
oraCommand.Parameters.Add(new OracleParameter("userName", domainUser));

来源:使用参数

这篇关于OracleCommand SQL 参数绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆