zoukankan      html  css  js  c++  java
  • 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();
    
            }
    
    
        }
    }
    


     

  • 相关阅读:
    centos7.2安装配置
    CentOS系统操作mysql的常用命令
    MySQL5.7安装与配置(YUM)
    排序的空间复杂度和尾递归小记
    常见内排序实现汇总(含部分优化实现,基于链表的实现),以及性能比较
    [ASP.NET]从ASP.NET Postback机制,到POST/GET方法
    内存管理笔记(分页,分段,逻辑地址,物理地址与地址转换方式)
    [EXT JS]"hasMany" association on ExtJS 4.1.1a
    使用nodejs抓取博客园内容---Promise模块探索
    NodeJs + mongodb模块demo
  • 原文地址:https://www.cnblogs.com/suixufeng/p/3336061.html
Copyright © 2011-2022 走看看