Oracle database table in gridview
To bind a DataTable
to a DataGridView
your code need simply to be changed to
public void read()
{
try
{
using(OracleConnection conn = new OracleConnection("....."))
using(OracleCommand cmd = new OracleCommand("select * from t1", conn))
{
conn.Open();
using(OracleDataReader reader = cmd.ExecuteReader())
{
DataTable dataTable = new DataTable();
dataTable.Load(reader);
dataGridView1.DataSource = dataTable;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
The OracleDataReader could be passed to the Load method of the DataTable and then the table is ready to be bound to the DataGridView DataSource property. I have also added some using statement to ensure proper disposing of the disposable objects employed. (In particular the OracleConnection is very expensive to not close in case of exceptions)
Loko
26 years old top 5% of php top 10% of mysql and html top 20% of laravel http://data.stackexchange.com/stackoverflow/query/52751/tag-rankings Software Developer September 2019 - Currently Software developer - Laravellaravel Reactjsreactjs Magentomagento Wordpresswordpress NextJSnextjs Typescripttypescript Sasssass November 2016 - August 2019 Software developer - PHPphp April 2016 - October 2016 Junior PHP Developer - PHPphp Symfonysymfony July 2015 - September 2015 Software developer - Laravellaravel PHPphp Finished school and got a degree in software engineering in June 2015 January 2015 - June 2015 Intern - Laravellaravel PHPphp June 2013 - January 2014 Intern - C#c# Oracleoracle GISgis PHP CSS HTML MYSQL Javascript Basic Linux Very little Gis and Oracle experience
Updated on July 05, 2022Comments
-
Loko almost 2 years
I want to get the result from a query in my oracle database and put it in a gridview. Now my problem is, I have no idea how to output it in the gridview. I am using the gridview from the toolbox and my oracle connection is working. I also have the right
SELECT
query and I can output that in a listbox. I just have no idea how to do this in a gridview. I looked for it and I came across this: How to populate gridview with mysql? Although this doesn't help me.How can I output it in a gridview so that it looks exactly the same as a normal table in the oracle database?
What should I use and how?
This is my code:
public void read() { try { var conn = new OracleConnection("") conn.Open(); OracleCommand cmd = new OracleCommand("select * from t1", conn); OracleDataReader reader = cmd.ExecuteReader(); DataTable dataTable = new DataTable(); while (reader.Read()) { var column1 = reader["vermogen"]; column = (column1.ToString()); listBox1.Items.Add(column); } conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
-
David L almost 6 yearsConsider adding an explanation about how and why this is the answer. A code snippet alone does not help the OP.