GridView中的Oracle数据库表 [英] Oracle database table in gridview

查看:145
本文介绍了GridView中的Oracle数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的oracle数据库中的查询中获取结果并将其放在gridview中.现在我的问题是,我不知道如何在gridview中输出它.我正在使用工具箱中的gridview,并且我的oracle连接正在工作.我也有正确的SELECT查询,可以在列表框中输出该查询.我只是不知道如何在gridview中执行此操作.我寻找了它,然后发现了这个问题:如何用mysql填充gridview?尽管这对我没有帮助.

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.

如何在gridview中输出它,使其看起来与oracle数据库中的普通表完全相同?

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?

这是我的代码:

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);
        }

    }

推荐答案

要将DataTable绑定到DataGridView,只需将代码更改为

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);
        }
    }
 }

可以将OracleDataReader传递给DataTable的Load方法,然后准备将表绑定到DataGridView DataSource属性.我还添加了一些using语句,以确保正确处置所用的一次性物品. (特别是OracleConnection在发生异常情况下无法关闭非常昂贵)

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)

这篇关于GridView中的Oracle数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆