C#連接Oracle數(shù)據(jù)庫查詢數(shù)據(jù)
作者:佚名
Oracle數(shù)據(jù)庫中可以運(yùn)行C#的確方便了我們的開發(fā)過程,下面就教您C#連接Oracle數(shù)據(jù)庫查詢數(shù)據(jù)的實(shí)現(xiàn)方法,供您參考學(xué)習(xí)。
C#連接Oracle數(shù)據(jù)庫可以實(shí)現(xiàn)許多我們需要的功能,下面介紹的是C#連接Oracle數(shù)據(jù)庫查詢數(shù)據(jù)的方法,如果您對(duì)C#連接Oracle數(shù)據(jù)庫方面感興趣的話,不妨一看。
- 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是手工寫的。別的都是系統(tǒng)自動(dòng)生成的
- string constring = "data source=wzd;user=wzd;password=wzd;";//定義連接數(shù)據(jù)庫的字符串
- OracleConnection conn = new OracleConnection(constring);//進(jìn)行連接
- try
- {
- conn.Open();//打開指定的連接
- OracleCommand com = conn.CreateCommand();
- com.CommandText = "select name from mytable where card_no='0000000002'";//寫好想執(zhí)行的Sql語句
- OracleDataReader odr = com.ExecuteReader();
- while (odr.Read())//讀取數(shù)據(jù),如果返回為false的話,就說明到記錄集的尾部了
- {
- this.lbl.Text = odr.GetOracleString(0).ToString();//將讀取到的值顯示到定義的控件中。
- }
- odr.Close();//關(guān)閉reader.這是一定要寫的
- }
- catch
- {
- MessageBox.Show("erro");//如果發(fā)生異常,則提示出錯(cuò)
- }
- finally
- {
- conn.Close();//關(guān)閉打開的連接
- }
- #endregion
- }
- }
- }
【編輯推薦】
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)