2

查看我的代码,它总是通过 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;
    }

但是如果我像下面这样对函数进行硬编码,则没有任何例外:

    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.
4

1 回答 1

2

根据OracleDataAdapter.SelectCommand 属性的规范, 语法应该在 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;
于 2013-11-04T05:00:44.720 回答