• 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)
            {

            }

        }
    }

  • 相关阅读:
    2021寒假每日一题《棋盘挑战》
    2021寒假每日一题《货币系统》
    2021寒假每日一题《红与黑》
    2021寒假每日一题《字母图形》
    2021寒假每日一题《完全背包问题》
    2021寒假每日一题《找硬币》
    python 迭代器和生成器
    python for循环
    python集合
    python字符串常用操作
  • 原文地址:https://www.cnblogs.com/meetweb/p/1300581.html
Copyright © 2020-2023  润新知