c# - 使用C#访问Oracle DB时总是遇到ORA-00936缺少express

标签 c# oracle ado.net

看看我的代码,总是通过ORA-00936缺少表达式异常:

    public DataSet getStagingTableFields(string tableName)
    {
        DataSet ds = new DataSet();
        StringBuilder sb = new StringBuilder();
        sb.Append(" select table_name, column_name,data_type,data_length from user_tab_columns where table_name = @tableName");
        sb.Append(" order by column_name asc ");
        String sqlStr = sb.ToString();

        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlStr;

        OracleParameter paraTableName = new OracleParameter("@tableName", OracleDbType.Varchar2);
        paraTableName.Value = tableName;

        cmd.Parameters.Add(paraTableName);

        OracleDataAdapter oraDp = new OracleDataAdapter();
        oraDp.SelectCommand = cmd;
        oraDp.Fill(ds, "StagingTableFields");


        disPose();
        return ds;
    }

但是如果我像下面这样对函数进行硬编码,则没有任何异常(exception):

    public DataSet getStagingTableFields(string tableName)
    {
        DataSet ds = new DataSet();
        StringBuilder sb = new StringBuilder();
        sb.Append(" select table_name, column_name,data_type,data_length from user_tab_columns where table_name = 'company'");
        sb.Append(" order by column_name asc ");
        String sqlStr = sb.ToString();

        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlStr;

       /*OracleParameter paraTableName = new OracleParameter("@tableName", OracleDbType.Varchar2);
        paraTableName.Value = tableName;
        cmd.Parameters.Add(paraTableName);*/

        OracleDataAdapter oraDp = new OracleDataAdapter();
        oraDp.SelectCommand = cmd;
        oraDp.Fill(ds, "StagingTableFields");


        disPose();
        return ds;
    }

为什么使用参数会出现这个异常,如何解决? 错误消息如下所示:

Oracle.DataAccess.Client.OracleException was unhandled
  Message=ORA-00936: missing expression
  Source=Oracle Data Provider for .NET
  ErrorCode=-2147467259
  DataSource=orcl
  Number=936
  Procedure=""
  StackTrace:
       at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck)
       at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, Boolean bCheck)
       at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
       at Oracle.DataAccess.Client.OracleDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
       at WindowsFormsApplication1.DAO.StagingTableFieldsDAO.getStagingTableFields(String tableName) in C:\Documents and Settings\zhanzhex\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\DAO\StagingTableFieldsDAO.cs:line 39
       at WindowsFormsApplication1.Service.FieldService.getFieldsList(String tableName) in C:\Documents and Settings\zhanzhex\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Service\FieldService.cs:line 29
       at WindowsFormsApplication1.Form1.fillStagingTableFieldsList() in C:\Documents and Settings\zhanzhex\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 119
       at WindowsFormsApplication1.Form1.comboStagingTables_SelectionChangeCommitted(Object sender, EventArgs e) in C:\Documents and Settings\zhanzhex\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 138
       at System.Windows.Forms.ComboBox.OnSelectionChangeCommitted(EventArgs e)
       at System.Windows.Forms.ComboBox.OnSelectionChangeCommittedInternal(EventArgs e)
       at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmCommand(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmCommand(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication1.Program.Main() in C:\Documents and Settings\zhanzhex\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

最佳答案

根据 OracleDataAdapter.SelectCommand Property 的规范语法应在 sqlString 中使用冒号 (:) 代替 @,并删除 OracleParameter 中的 @,如下所示

    StringBuilder sb = new StringBuilder();
    sb.Append("select table_name, column_name, data_type, data_length from user_tab_columns where table_name = :tableName");
    sb.Append(" order by column_name asc ");
    String sqlStr = sb.ToString();

    OracleParameter paraTableName = new OracleParameter("tableName", OracleDbType.Varchar2);
    paraTableName.Value = tableName;

关于c# - 使用C#访问Oracle DB时总是遇到ORA-00936缺少express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19761567/

相关文章:

c# - 如何使用 JSON.NET 解析带有十六进制编码字符的格式错误的 JSONP?

SQL 如何在 SUM 中使用 LAG

database - 我们可以在每天使用作业重新加载的 Oracle 11 中的表列上创建位图索引吗

sql - 查找覆盖两个日期之间的员工的更有效方法

c# - 将 DataTable 作为参数传递给存储过程

c# - 如何检查播放器和 Unity 中特定 TileBase 对象之间的碰撞?

c# - 并行调用 IEnumerable 的元素

c# - 哪个是首选 : new Nullable<int> or (int? )null?

c# - SET READ_COMMITTED_SNAPSHOT ON 的 ADO.Net IsolationLevel.Snapshot

c# - TableAdapter 的插入方法不起作用?