34

我对以下参数的绑定有疑问。连接有效,因为我在没有使用参数的情况下对其进行了测试。但是,执行之前的查询值仍然使用“@userName”而不是“jsmith”。

问题是什么?这不是绕过绑定的正确方法吗?

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;
}

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

4

5 回答 5

84

删除 @username 周围的单引号,并且对于 oracle 使用:参数名称而不是@,例如:

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

来源:使用参数

于 2012-06-15T10:28:49.660 回答
22

Oracle 的参数语法与 Sql-Server 不同。所以使用:而不是@

using(var con=new OracleConnection(connectionString))
{
   con.open();
   var sql = "insert into users values (:id,:name,:surname,:username)";

   using(var cmd = new OracleCommand(sql,con)
   {
      OracleParameter[] parameters = new OracleParameter[] {
             new OracleParameter("id",1234),
             new OracleParameter("name","John"),
             new OracleParameter("surname","Doe"),
             new OracleParameter("username","johnd")
      };

      cmd.Parameters.AddRange(parameters);
      cmd.ExecuteNonQuery();
   }
}

在 OracleCommand 中使用命名参数时,必须在参数名称前加上冒号 (:)。

http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oraclecommand.parameters.aspx

于 2013-11-09T12:30:27.727 回答
1

你需要使用这样的东西:

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

更多可以在这篇 MSDN 文章中找到:http: //msdn.microsoft.com/en-us/library/system.data.oracleclient.oraclecommand.parameters%28v=vs.100%29.aspx

建议您在 Oracle 中使用:字符而不是@

于 2012-10-11T13:34:26.227 回答
1
string strConn = "Data Source=ORCL134; User ID=user; Password=psd;";

System.Data.OracleClient.OracleConnection con = newSystem.Data.OracleClient.OracleConnection(strConn);
    con.Open();

    System.Data.OracleClient.OracleCommand Cmd = 
        new System.Data.OracleClient.OracleCommand(
            "SELECT * FROM TBLE_Name WHERE ColumnName_year= :year", con);

//for oracle..it is :object_name and for sql it s @object_name
    Cmd.Parameters.Add(new System.Data.OracleClient.OracleParameter("year", (txtFinYear.Text).ToString()));

    System.Data.OracleClient.OracleDataAdapter da = new System.Data.OracleClient.OracleDataAdapter(Cmd);
    DataSet myDS = new DataSet();
    da.Fill(myDS);
    try
    {
        lblBatch.Text = "Batch Number is : " + Convert.ToString(myDS.Tables[0].Rows[0][19]);
        lblBatch.ForeColor = System.Drawing.Color.Green;
        lblBatch.Visible = true;
    }
    catch 
    {
        lblBatch.Text = "No Data Found for the Year : " + txtFinYear.Text;
        lblBatch.ForeColor = System.Drawing.Color.Red;
        lblBatch.Visible = true;   
    }
    da.Dispose();
    con.Close();
于 2013-09-28T08:47:01.497 回答
0

这是我使用Oracle.DataAccess.Client命名空间解决相同问题的方法。

using Oracle.DataAccess.Client;


string strConnection = ConfigurationManager.ConnectionStrings["oConnection"].ConnectionString;

dataConnection = new OracleConnectionStringBuilder(strConnection);

OracleConnection oConnection = new OracleConnection(dataConnection.ToString());

oConnection.Open();

OracleCommand tmpCommand = oConnection.CreateCommand();
tmpCommand.Parameters.Add("user", OracleDbType.Varchar2, txtUser.Text, ParameterDirection.Input);
tmpCommand.CommandText = "SELECT USER, PASS FROM TB_USERS WHERE USER = :1";

try
{
    OracleDataReader tmpReader = tmpCommand.ExecuteReader(CommandBehavior.SingleRow);
    
    if (tmpReader.HasRows)
    {
        // PT: IMPLEMENTE SEU CÓDIGO    
        // ES: IMPLEMENTAR EL CÓDIGO
        // EN: IMPLEMENT YOUR CODE
    }
}
catch(Exception e)
{
        // PT: IMPLEMENTE SEU CÓDIGO    
        // ES: IMPLEMENTAR EL CÓDIGO
        // EN: IMPLEMENT YOUR CODE
}
于 2017-01-30T16:08:32.487 回答