We have a US7ASCII 11gr2 database that I'm trying to build parameterised queries against.
public CustDetailsModel SelectCustDetails(string CustCode)
{
string sql;
OracleDataReader reader;
OracleConnection OraConn = OraConnection.GetConnection;
OraConn.Open();
sql = "SELECT CUSTOMER, NAME, POSTCODE FROM CUSTOMER WHERE CUSTOMER = :CUSTCODE";
OracleCommand cmd = new OracleCommand(sql, OraConn);
OracleParameter parameter = cmd.CreateParameter();
parameter.ParameterName = "CUSTCODE";
parameter.OracleDbType = OracleDbType.NVarchar2;
parameter.Value = CustCode;
cmd.Parameters.Add(parameter);
reader = cmd.ExecuteReader();
reader.Read();
return new CustDetailsModel
{
Cust = reader.GetString(reader.GetOrdinal("CUSTOMER")),
CustName = reader.GetString(reader.GetOrdinal("NAME")),
CustPostCode = reader.GetString(reader.GetOrdinal("POSTCODE")),
};
}
At the add parameter line, the above code triggers a System.InvalidCastException "Unable to cast object of type 'System.String' to type 'Oracle.ManagedDataAccess.Client.OracleParameter".
I've tried everything I can find on the internet including stringbuilder, bind by name, different dbtypes etc.
Does anybody know how to set up a string bind variable in C# to query against an ASCII oracle db?
Thanks in advance.
EDIT I should explain that I know it is something to do with the disparate types of the db and application because we have successfully used int bind variables and, we can also replace the string bind variables with hardcoded values. We can also replace string bind variables with regular string variables (which we're trying to avoid). It seems to be an issue with the conversion of the bind variable when it hits the database?