通过odbc命令在oracle查询中缺少表达

时间:2015-01-15 05:19:08

标签: c# asp.net oracle odbc

我使用oracle数据库作为后端。当我在asp.net c#中运行以下脚本时出现以下错误

sql = "SELECT * FROM STUDENT WHERE YEAR = @YEARS AND SEMESTER = @SEMESTER";
 OdbcCommand cmd = new OdbcCommand(sql, connection);
                cmd.Parameters.AddWithValue("@YEARS", 2010);
                cmd.Parameters.AddWithValue("@SEMESTER", 2);
                OdbcDataAdapter da = new OdbcDataAdapter(cmd);
                DataTable dtf = new DataTable();
                da.Fill(dtf);

当我运行此脚本时出现以下错误

错误[42000] [微软] [适用于Oracle的ODBC驱动程序] [Oracle] ORA-00936:缺少表达式

上面的代码是什么问题。

3 个答案:

答案 0 :(得分:0)

YEARODBC中的保留字,请尝试将其括在方括号[]内。

请参阅ODBC Reserved Words

试试这个:

sql = "SELECT * FROM STUDENT WHERE [YEAR] = @YEAR AND SEMESTER = @SEMESTER";
OdbcCommand cmd = new OdbcCommand(sql, connection);
            cmd.Parameters.AddWithValue("@YEAR", 2010);
            cmd.Parameters.AddWithValue("@SEMESTER", 2);
            OdbcDataAdapter da = new OdbcDataAdapter(cmd);
            DataTable dtf = new DataTable();
            da.Fill(dtf);

答案 1 :(得分:0)

删除查询中的@。在Oracle中,绑定变量与使用冒号:BIND_VARIABLE

(:)相似
sql = "SELECT * FROM STUDENT WHERE YEAR = :YEAR AND SEMESTER = :SEMESTER";
 OdbcCommand cmd = new OdbcCommand(sql, connection);
                cmd.Parameters.AddWithValue("YEAR", 2010);
                cmd.Parameters.AddWithValue("SEMESTER", 2);
                OdbcDataAdapter da = new OdbcDataAdapter(cmd);
                DataTable dtf = new DataTable();
                da.Fill(dtf);

答案 2 :(得分:0)

也许你可以尝试使用"?"

    sql = "SELECT * FROM STUDENT WHERE YEAR = ? AND SEMESTER = ?";
           OdbcCommand cmd = new OdbcCommand(sql, connection);
           cmd.Parameters.AddWithValue("?", 2010);
           cmd.Parameters.AddWithValue("whatever", 2); 
           OdbcDataAdapter da = new OdbcDataAdapter(cmd);
           DataTable dtf = new DataTable();
           da.Fill(dtf);

you can look this

我试过了

    sql = "SELECT * FROM STUDENT WHERE YEAR = :YEAR AND SEMESTER = :SEMESTER";
    OdbcCommand cmd = new OdbcCommand(sql, connection);
    cmd.Parameters.AddWithValue(":YEAR", 2010);
    //

没有工作。