C#串連Oracle資料庫字串【51cto線上經典書籍】

來源:互聯網
上載者:User

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();                }            }        }   } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.