winform操作访问Oracle 10g数据库,并自动填充到DataGridView

使用oracle的ODP.NET是官方推荐,而且相对简单的方法。

官方指导文档:

http://www.oracle.com/technetwork/cn/testcontent/o23odp-084525-zhs.html

 

app.config

<?xml version="1.0"?>
<configuration>
  <appSettings>

    <add key="ORACLE" value="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.133)(PORT=1521))(CONNECT_DATA=(SID=yy)));User Id=system;Password=orcl;"/>
  </appSettings>

  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>


Form1.aspx.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;

using Oracle.DataAccess.Client;

namespace JiaJiayue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        string connstring =  ConfigurationSettings.AppSettings["ORACLE"]; 
  

        //private DataGridView dataGridView1 = new DataGridView();
        private BindingSource bindingSource1 = new BindingSource();
        private OracleDataAdapter dataAdapter = new OracleDataAdapter();


        //string connstring = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.133)(PORT=1521))(CONNECT_DATA=(SID=yy)));User Id=system;Password=orcl;";

        private void btnSearch_Click(object sender, EventArgs e)
        {
           
            dataGridView1.DataSource = bindingSource1;
            if (txtName.Text == "")
            {
                GetData("select  tbm_psndoc.timecardid,psnname,deptname  from tbm_psndoc left join bd_psndoc on tbm_psndoc.pk_psndoc=bd_psndoc.pk_psndoc left join bd_deptdoc on bd_psndoc.pk_deptdoc=bd_deptdoc.pk_deptdoc");
            }
            else
            {
                GetData("select  tbm_psndoc.timecardid,psnname,deptname  from tbm_psndoc left join bd_psndoc on tbm_psndoc.pk_psndoc=bd_psndoc.pk_psndoc left join bd_deptdoc on bd_psndoc.pk_deptdoc=bd_deptdoc.pk_deptdoc where psnname='" + txtName.Text + "'");
            }

        }

        private void GetData(string selectCommand)
        {
            try
            {
              

                // Create a new data adapter based on the specified query.
                dataAdapter = new OracleDataAdapter(selectCommand, connstring);

                // Create a command builder to generate SQL update, insert, and
                // delete commands based on selectCommand. These are used to
                // update the database.
                OracleCommand commandBuilder = new OracleCommand();

                // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;

                // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns(
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch
            {
               
            }
        }

 

        private void 更新ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form2 formone = new Form2();
            formone.Show();

        }


    }
}


 

原文地址:https://www.cnblogs.com/suixufeng/p/3336061.html