我编写了一个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()方法中抛出错误“缺少表达式”,有人可以告诉我为什么吗?我很困惑

提前致谢!

最佳答案

使用:代替@

 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

关于c# - Oracle.DataAccess.Client.OracleException:ORA-00936:缺少表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40416130/

10-09 18:18