问题
When I run this code it always returning zero while when i run this query in oracle sql client it returns 1.
var strSQL = string.Format("SELECT COUNT(*) FROM GANTNER.GAT_REASONS WHERE CODE ='{0}'", StatusCode);
objCmd = new OracleCommand(strSQL, objConn);
objCmd.CommandType = CommandType.Text;
int val = int.Parse(objCmd.ExecuteScalar().ToString());
For try I remove condition on column name CODE
and it works but when I put column name CODE
it does not then I tried to put "CODE"
'CODE'
all not working always returning zero while it should return 1 because row exist.
回答1:
May be letter case problem.Try UPPER/UCASE
function to compare.
var strSQL = string.Format("SELECT COUNT(*) FROM GANTNER.GAT_REASONS
WHERE UCASE(CODE) ='{0}'", StatusCode.ToUpper());
Or
var strSQL ="SELECT COUNT(*) FROM GANTNER.GAT_REASONS WHERE CODE =:code";
objCmd = new OracleCommand(strSQL, objConn);
objCmd.Parameters.AddWithValue("code",StatusCode.ToUpper());
回答2:
Try this:
OracleCommand cmd = conn.createCommand();
// parametrized sql query
cmd.CommandText = "SELECT COUNT(*) FROM GANTNER.GAT_REASONS WHERE CODE = :status";
// add parameter for the query
cmd.Parameters.AddWithValue("status", StatusCode);
int val = int.Parse(objCmd.ExecuteScalar().ToString());
来源:https://stackoverflow.com/questions/11664253/oracle-query-is-not-working-in-c-sharp