zoukankan      html  css  js  c++  java
  • DotNet 连接 Oracle 10G数据库的测试

    Oracle 10G的安装请见上一篇文章,现在是安装后测试一下连接的效果

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

    namespace Nhibernate1
    {
        public partial class FormOracle : Form
        {
            private System.Data.OracleClient.OracleConnection con;
            private System.Data.OracleClient.OracleDataAdapter adapter;
            private System.Data.OracleClient.OracleCommand cmd;
            private System.Data.DataSet ds;

            public FormOracle()
            {
                InitializeComponent();
            }

            private void FormOracle_Load(object sender, EventArgs e)
            {
                openCon();
                DataSet ds = GetDataSet(" select empno,ename,job from scott.emp");
                this.dataGridView1.DataSource = ds.Tables[0];
            }

             private void openCon()
            {
                 try
                 {
                     if (this.con == null)
                     {
                        //使用using可以使该连接可以调用Dispose方法来释放资源
                        //using (this.con = new OracleConnection())
                        //{
                           this.con = new OracleConnection();
                            //设置数据库连接属性为web.config中的设置的值(默认)
                            //或者设置为构造函数中指定的connString的值
                            this.con.ConnectionString
                                = "Data Source=orcl;User ID=scott;Password=tiger;Unicode=True";
                            this.con.Open();
                        //}
                        System.Console.Write("数据库连接成功!"); //Test
                    }
                    else if (con.State == ConnectionState.Closed)
                    {
                        this.con.Open();
                    }
                }
                catch
                {
                   System.Console.Write("数据库连接失败,请与管理员联系!");
                   
                }
            }

            public System.Data.DataSet GetDataSet(string sql)
            {
                this.adapter = new OracleDataAdapter();
                this.ds = new DataSet();
                this.CreateCmd(sql);
                this.adapter.SelectCommand = this.cmd;
                this.adapter.Fill(this.ds);
                return this.ds;
            }

            private void CreateCmd(string sql)
            {
                //方法1
                this.cmd = new OracleCommand();

                this.cmd.Connection = this.con;
                this.cmd.CommandText = sql;
          }

            private void button1_Click(object sender, EventArgs e)
            {

            }

        }
    }

  • 相关阅读:
    网络芯片应用:GPS公交车行驶记录仪
    “黑暗潜伏者” -- 手机病毒新型攻击方式
    Android导入工程提示Invalid project description
    设计模式_代理模式
    el简略说明与11个隐含对象
    jsp九大内置对象
    js全局函数
    python基础:multiprocessing的使用
    pgAdmin III 使用图解
    windows下简单安装postgres
  • 原文地址:https://www.cnblogs.com/meetweb/p/1300581.html
Copyright © 2011-2022 走看看