在 gridview 中显示 oracle 数据库值

Posted

技术标签:

【中文标题】在 gridview 中显示 oracle 数据库值【英文标题】:Showing oracle database values into gridview 【发布时间】:2022-01-11 02:31:08 【问题描述】:

您好,我有一个带有 3 列 NOME、COGNOME、NUMTELEFONO 的表 PROVA 的 oracle 数据库。 我正在搜索用包含在表中的值来更新我在 c# 中的 gridview。 这是我的代码,在 button1_Click() 方法中,我需要在我的 gridview c# 应用程序中显示 db 值。有人可以帮我写代码吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.ManagedDataAccess;



namespace dbOracleForm

    public partial class Form1 : Form
    
        

        public Form1()
        
            InitializeComponent();
        

        private void label2_Click(object sender, EventArgs e)
        

        

        private void label1_Click(object sender, EventArgs e)
        

        

        private void button1_Click(object sender, EventArgs e)
        
            String connectionString = "Data Source =  (DESCRIPTION = "+
    "(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))" +
    "(CONNECT_DATA = " +
     " (SERVER = DEDICATED) " + 
     " (SERVICE_NAME = orcl.home) " +
    ")"+
  "); User Id = system;password = orcl;";
            OracleConnection  con = new OracleConnection();
            con.ConnectionString = connectionString;

            con.Open();

            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = "SELECT NOME, COGNOME, NUMTELEFONO FROM PROVA ORDER BY COGNOME DESC";
            cmd.CommandType = CommandType.Text;
            OracleDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataGridView1.DataSource(dt);



        


        private void Form1_Load(object sender, EventArgs e)
        

        
    

【问题讨论】:

以下内容可能会有所帮助:Oracle .NET Developer Center、Introducing Oracle Data Provider for .NET 和 Using ODP.NET Client Provider in a Simple Application 这是另一个可能有用的帖子:docs.oracle.com/cd/E20434_01/doc/win.112/e23174/… 【参考方案1】:

你可以这样做

 private void button1_Click(object sender, EventArgs e)
    
       ...

        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = "SELECT NOME, COGNOME, NUMTELEFONO FROM PROVA ORDER BY COGNOME DESC";
        cmd.CommandType = CommandType.Text;
        OracleDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);

        dataGridView1.DataSource(dt);//wrong
        dataGridView1.DataSource= dt;//correct



    

【讨论】:

以上是关于在 gridview 中显示 oracle 数据库值的主要内容,如果未能解决你的问题,请参考以下文章

GridView 中 order by desc 排序失效??

gridview如何动态添加列,如何在添加列后显示数据

在Gridview中显示Gridview

在 Gridview 中显示 Gridview

Yii2 Oracle日期列

[转]Oracle分页之三:利用PagerView来实现无刷新GridView