C#串連Oracle資料庫字串
【轉】http://developer.51cto.com/art/200908/145365.htm
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 } } }
------------------------------------------------------------------------------------------------------------------------------------------------
C#串連Oracle資料庫字串的代碼
------------------------------------------------------------------------------------------------------------------------------------------------
C#串連Oracle資料庫(更改資料庫中的記錄並查詢更改後的資料)
using System; using System.Collections.Generic; using System.ComponentModel;//這行和下一行都要先在引用中填加system.data.oracleclient using 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 fin_ipr_inmaininfo 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 } private void button2_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 = "update fin_ipr_inmaininfo set name='wzd' where card_no='0000000002'";//寫好想執行的Sql語句 com.ExecuteNonQuery(); } catch { MessageBox.Show("erro");//如果發生異常,則提示出錯 } finally { conn.Close();//關閉開啟的串連 } #endregion } } }
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
注意:一定要添加這個:
項目->添加引用->.NET->System.Data.OracleClient.dll
using System; using System.Data; using System.Windows.Forms; using System.Data.OracleClient; namespace Test ...{ /**//**//**//// /// 簡潔期間,直接將實現寫在建構函式中 /// public class Test ...{ public Test() ...{ // // TODO: 在此處添加建構函式邏輯 // string ConnectionString = "Data Source=LiPu; User Id=SCOTT; Password=scott"; //C#串連Oracle資料庫字串,Data Source 是指資料庫名字.如我用的是原生Oracle 的資料庫,名字為LiPu. user id 是 //使用者名稱,你可以用System 或是你自己添加的一個使用者.Password是對應使用者的密碼. OracleConnection conn = new OracleConnection(ConnectionString); //建立一個新串連 try { conn.Open(); //開啟串連 OracleCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from emp"; //SQL語句 OracleDataReader rs = cmd.ExecuteReader(); while (rs.Read()) //讀取資料,如果rs.Read()返回為false的話,就說明到記錄集的尾部了 { MessageBox.Show(rs.GetString(1)); } rs.Close(); } catch (Exception e) ...{ MessageBox.Show(e.Message); } finally ...{ conn.Close(); } } } }