2013-08-27 42 views
0

我想通過ODP.net(Oracle數據提供商)從遠程Oracle數據庫搜索數據連接到遠程數據庫,但是我總是有以下錯誤消息:如何通過DB連接使用ADO.net

Oracle.DataAccess.Client.OracleException was unhandled 
    Message=ORA-00936: missing expression 
    Source=Oracle Data Provider for .NET 
    ErrorCode=-2147467259 
    DataSource=RETLMCC_ETLAPPL 
    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.OracleCommand.ExecuteReader() 
     at WindowsFormsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\zhanzhex\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 52 
     at System.Windows.Forms.Form.OnLoad(EventArgs e) 
     at System.Windows.Forms.Form.OnCreateControl() 
     at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 
     at System.Windows.Forms.Control.CreateControl() 
     at System.Windows.Forms.Control.WmShowWindow(Message& m) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
     at System.Windows.Forms.ContainerControl.WndProc(Message& m) 
     at System.Windows.Forms.Form.WmShowWindow(Message& m) 
     at System.Windows.Forms.Form.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.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow) 
     at System.Windows.Forms.Control.SetVisibleCore(Boolean value) 
     at System.Windows.Forms.Form.SetVisibleCore(Boolean value) 
     at System.Windows.Forms.Control.set_Visible(Boolean value) 
     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: 

下面是我的代碼:

 private void Form1_Load(object sender, EventArgs e) 
    { 
     //listBox1.Items.Add(new System.Windows.Forms.CheckBox()); 
     System.Windows.Forms.CheckBox chb1 = new System.Windows.Forms.CheckBox(); 
     chb1.Text = "test"; 
     checkedListBox1.Items.Add("test1"); 
     checkedListBox1.Items.Add("test2"); 
     checkedListBox1.Items.Add("test3");//RETLMCC_ETLAPPL 
     string oradb = "Data Source=RETLMCC_ETLAPPL;User Id=ABC;Password=ABC;"; 
     OracleConnection conn = new OracleConnection(); 
     conn.ConnectionString = oradb; 
     StringBuilder sb = new StringBuilder(); 
     sb.Append(" select table_name, column_name,data_type,data_length "); 
     sb.Append(" from [email protected]_LINK where table_name = @tableName order by column_name asc"); 
     string sqlStr = sb.ToString(); 
     //RETLMCC_ETLAPPL_SM_3_11 
     OracleCommand cmd = new OracleCommand(); 

     cmd.CommandText = sqlStr;//"select name from ovsc_country_out"; 
     cmd.Connection = conn; 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection.Open(); 

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

     OracleParameter paraDBLink = new OracleParameter("@DB_LINK", OracleDbType.Char); 
     paraDBLink.Value = "@RETLMCC_ETLAPPL_SM_3_11"; 

     cmd.Parameters.Add(paraDBLink); 
     cmd.Parameters.Add(paraTableName); 

     OracleDataReader dr = cmd.ExecuteReader(); 
     while (dr.Read()) 
     { 
      checkedListBox1.Items.Add(dr["column_name"].ToString()); 
     } 
     if (conn.State == ConnectionState.Open) 
     { 
      conn.Close(); 
     } 
    } 

如果我搜索本地數據庫而不是遠程數據庫,代碼工作很好,但我怎麼能弄清楚當我使用DB鏈接在我的SQL字符串?

回答

0

不能將數據庫鏈接的名稱作爲參數傳遞,就像無法將表名稱作爲參數一樣傳遞。

取而代之的是:

sb.Append(" from [email protected]_LINK where table_name = @tableName order by column_name asc"); 

OracleParameter paraDBLink = new OracleParameter("@DB_LINK", OracleDbType.Char); 
paraDBLink.Value = "@RETLMCC_ETLAPPL_SM_3_11"; 

使用本:

sb.Append(" from [email protected]_ETLAPPL_SM_3_11 where table_name = @tableName order by column_name asc"); 
+0

我改變了代碼就像你說的,但我得到了同樣的錯誤,當 cmd.ExecuteReader(); 正在運行。 順便說一句,如果數據庫鏈接是可變的,如何解決這個問題呢? – user2575502