Oracle.DataAccess.Client.OracleException:ORA-00936:缺少表达式

时间:2016-11-04 05:53:11

标签: c# oracle ado.net

我写了一个C#控制台程序试图从Oracle查询一些数据,这是一个非常简单的查询,但我不知道为什么它总是在运行时告诉我“缺少表达式”,请参阅下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Configuration;

namespace ConnectToOracle
{
    class Program
    {
        static void Main(string[] args)
        {

            string strCon = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (OracleConnection oc = new OracleConnection(strCon))
            {
                OracleCommand cmd = new OracleCommand("select dname from dept where deptno = @deptno", oc);
                OracleParameter op = new OracleParameter();
                op.ParameterName = "@deptno";
                op.OracleDbType = OracleDbType.Int32;
                op.Direction = System.Data.ParameterDirection.Input;

                cmd.Parameters.Add(op);
                oc.Open();
                string dname = (string)cmd.ExecuteScalar();
           }
        }
    }
}

所以,在最后一行,将从cmd.ExecuteScalar()方法抛出错误“missing expression”,任何人都可以告诉我为什么?我很困惑

提前感谢!

1 个答案:

答案 0 :(得分:3)

使用:代替@

 OracleCommand cmd = new OracleCommand("select dname from dept where deptno = :deptno", oc);
                OracleParameter op = new OracleParameter();
                op.ParameterName = "deptno";
                op.OracleDbType = OracleDbType.Int32;
                op.Direction = System.Data.ParameterDirection.Input;

参考:OracleCommand.Parameters Property