C#连接oracle数据库

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

//测试数据库连接
namespace dbTest
{
    public partial class Form1 : Form
    {
        string[] table;
        int i = 0;

        public Form1()
        {
            table = new string[20];
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //测试,写字符串到文件(文件名:2012-10-10)
            string checkStr = "select count(*) from emp";           //时间命名文件
            string myFileName = "c:\\" + DateTime.Today.ToShortDateString() + ".txt";
            //建立文件流
            FileStream fs = new FileStream(myFileName, FileMode.OpenOrCreate);
            //建立输入流对象
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(checkStr);
            sw.Flush();
            sw.Close();
            try
            {
                //连接 用OracleCommand方法进行简单查询
                  string ConString = "Data Source=TEST;user=TEST001;password=123456";
                OracleConnection OraConn = new OracleConnection(ConString);
                //查询语句
                OracleCommand OraCmd = new OracleCommand("select empno from emp", OraConn);
                //打开数据连接
                OraConn.Open();

                //存储过程测试
                  //OraCmd.CommandText="proc_test";
                //OraCmd.CommandType=CommandType.StoredProcedure;
                //OracleCommandBuilder.DeriveParameters(OraCmd);
             
                DataSet ds1;
                ds1 = new DataSet();
                OracleDataAdapter da1 = new OracleDataAdapter(OraCmd);//取出数据
                   da1.Fill(ds1);
                DataTable dt = ds1.Tables[0];
                this.dataGridView1.DataSource = dt.DefaultView;
                this.dataGridView1.Refresh();//刷新数据控件
                //关闭数据连接
                OraConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了!!");
            }

        }

    }

    
}

 

转载于:https://www.cnblogs.com/Cryking/archive/2012/11/13/2767692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值