I have the following code:
int count = 0;
OracleDataReader dr;
parameters = new Dictionary<string, string>();
parameters.Add("Username", UserName);
query = "Select KEY from TABLE1 where USERID=:Username AND KEY IN(";
foreach (string Location in Locations)
{
query = query + ":Loc" + count.ToString() + ",";
parameters.Add("Loc" + count.ToString(), Location);
count++;
}
query=query.TrimEnd(',');
query = query + ")";
dr=objConexion.ExecuteQuery(query, parameters, objConexion.OracleCnn);
public OracleDataReader ExecuteQuery(String query, Dictionary<string,string> Parameters, OracleConnection oracleCnn)
{
OracleDataReader OracleDr;
OracleCommand OracleCmd;
try
{
OracleCmd = oracleCnn.CreateCommand();
OracleCmd.CommandText = query;
foreach (KeyValuePair<string, string> kvp in Parameters)
{
OracleCmd.Parameters.AddWithValue(kvp.Key, kvp.Value);
}
OracleCmd.Prepare();
OracleDr = OracleCmd.ExecuteReader();
}
catch
{
OracleDr = null;
}
return OracleDr;
}
For some reason the code works fine sometimes and others it doesn´t, After debugging I noticed that when it doesn´t work, the Datareader has an exception on its "HasRows" method, specifically an Invalid Number Exception. I have tried running the resulting query directly on the DBMS and it executes perfectly, does anyone know why this exception is happening? what am I missing?.
Do the following to find issue:
OracleCmd.Parameters.AddWithValue(kvp.Key, int.Parse(kvp.Value));
I think, you passing some "bad integer" in one of the locations See this sql error "ORA-01722: invalid number"
And please, remove
OracleCmd.Prepare();
Another thing, why do you pass connection to
public OracleDataReader ExecuteQuery(String query,
Dictionary<string,string> Parameters,
OracleConnection oracleCnn)
Pass a connection string and open connection before execution. Use appropriate CommandBehavior
MSDN : [CommandBehavior.CloseConnection]When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.