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("出错了!!");
            }

        }

    }

    
}

 

posted @ 2012-11-13 11:30  cryking  阅读(541)  评论(0编辑  收藏  举报