2017-04-26 81 views
0

我想在Windows窗體應用程序的DataGridView中顯示Oracle Data,但它只是返回一個灰色的空白視圖。我給這家代碼當前是:如何在GridView中使用TextBox值在Oracle數據庫中搜索數據

 string insertquery = "select * from Candidate where CandidateName like '"+ txtBoxSearchData.Text +"%'"; 

     OracleConnection con = new OracleConnection(oradb); 
     con.Open(); 
     OracleCommand cmd = new OracleCommand(); 
     cmd.Connection = con; 
     cmd.CommandText = insertquery; 

     try 
     { 
      OracleDataReader reader = cmd.ExecuteReader(); 

      OracleDataAdapter orada = new OracleDataAdapter(cmd); 
      DataTable dataTable = new DataTable(); 
      orada.Fill(dataTable); 
      dataTable.Load(reader); 
      BindingSource bSource = new BindingSource(); 
      bSource.DataSource = dataTable; 
      dataGridViewSearch.DataSource = bSource; 
      orada.Update(dataTable); 

     } 
     catch(ArgumentException ex) 
     { 
      MessageBox.Show("Error: " + ex.Message); 
     } 
     catch(OracleException ex1) 
     { 
      MessageBox.Show("Error: " + ex1.Message); 
     } 
     finally 
     { 
      cmd.Dispose(); 
      con.Dispose(); 
     } 
    } 

我肯定我不要求我嘗試語句中的所有這些功能,但我也碰到過許多不同的方法來做到這一點 - 我只是包括了所有那些爲我的代碼實驗。連接字符串也是正確的,因爲我成功地通過我的應用程序的另一部分中的查詢將數據添加到表中。

回答

0

做下面的事情應該足夠了。應該不需要OracleDataAdapter或BindingSource。只是不能在這裏測試,因爲我沒有Oracle數據庫。

public void fillDataGrid() 
    { 
     try 
     { 
      using(OracleConnection connection = new OracleConnection("connectstring")) 
      using(OracleCommand cmd = new OracleCommand("select * from my super table", connection)) 
      { 
       connection .Open(); 
       using(OracleDataReader oracleDataReader = cmd.ExecuteReader()) 
       { 
        DataTable dataTable = new DataTable(); 
        dataTable.Load(oracleDataReader); 
        myDataGrid.DataSource = dataTable; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 
相關問題