您的位置:首页 > 数据库 > Oracle

DotNet 连接 Oracle 10G数据库的测试

2008-09-27 15:11 399 查看
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)

{

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐