2

I wrote a C# console program to try to query some data from Oracle, it is a very simple query but I don't know why it is always tell me "missing expression" while running it, see below my code

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();
           }
        }
    }
}

so, at the last line, the error "missing expression" will be thrown from cmd.ExecuteScalar() method, can any one tell me why? I was confused

thanks in advance!

0

1 Answer 1

13

Use : instead of @.

 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;

Reference:OracleCommand.Parameters Property

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.