C#连接Oracle数据库可以实现许多我们需要的功能,下面介绍的是C#连接Oracle数据库查询数据的方法,如果您对C#连接Oracle数据库方面感兴趣的话,不妨一看。
using System;
using System.Collections.Generic;
using System.ComponentModel
using System.Data.OracleClient;;//这行和下一行都要先在引用中填加system.data.oracleclient
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region 从region到endregion是手工写的。别的都是系统自动生成的
string constring = "data source=wzd;user=wzd;password=wzd;";//定义连接数据库的字符串
OracleConnection conn = new OracleConnection(constring);//进行连接
try
{
conn.Open();//打开指定的连接
OracleCommand com = conn.CreateCommand();
com.CommandText = "select name from mytable where card_no='0000000002'";//写好想执行的Sql语句
OracleDataReader odr = com.ExecuteReader();
while (odr.Read())//读取数据,如果返回为false的话,就说明到记录集的尾部了
{
this.lbl.Text = odr.GetOracleString(0).ToString();//将读取到的值显示到定义的控件中。
}
odr.Close();//关闭reader.这是一定要写的
}
catch
{
MessageBox.Show("erro");//如果发生异常,则提示出错
}
finally
{
conn.Close();//关闭打开的连接
}
#endregion
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
【编辑推荐】