Oracle query is not working in C#

a 夏天 提交于 2020-01-15 07:15:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!