新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C#連接Oracle數(shù)據(jù)庫(kù)字符串
C#連接Oracle數(shù)據(jù)庫(kù)字符串(查詢(xún)數(shù)據(jù))

創(chuàng)新互聯(lián)建站成都網(wǎng)站建設(shè)按需網(wǎng)站開(kāi)發(fā),是成都網(wǎng)站開(kāi)發(fā)公司,為成都格柵板提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開(kāi)發(fā)等。成都網(wǎng)站改版熱線(xiàn):13518219792
- 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是手工寫(xiě)的。別的都是系統(tǒng)自動(dòng)生成的
- string constring = "data source=wzd;user=wzd;password=wzd;";//定義連接數(shù)據(jù)庫(kù)的字符串
- OracleConnection conn = new OracleConnection(constring);//進(jìn)行連接
- try
- {
- conn.Open();//打開(kāi)指定的連接
- OracleCommand com = conn.CreateCommand();
- com.CommandText = "select name from mytable where card_no='0000000002'";//寫(xiě)好想執(zhí)行的Sql語(yǔ)句
- OracleDataReader odr = com.ExecuteReader();
- while (odr.Read())//讀取數(shù)據(jù),如果返回為false的話(huà),就說(shuō)明到記錄集的尾部了
- {
- this.lbl.Text = odr.GetOracleString(0).ToString();//將讀取到的值顯示到定義的控件中。
- }
- odr.Close();//關(guān)閉reader.這是一定要寫(xiě)的
- }
- catch
- {
- MessageBox.Show("erro");//如果發(fā)生異常,則提示出錯(cuò)
- }
- finally
- {
- conn.Close();//關(guān)閉打開(kāi)的連接
- }
- #endregion
- }
- }
- }
C#連接Oracle數(shù)據(jù)庫(kù)字符串的代碼
注意:一定要添加這個(gè):
項(xiàng)目->添加引用->.NET->System.Data.OracleClient.dll
- using System;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.OracleClient;
- namespace Test
- ...{
- /**//**//**////
- /// 簡(jiǎn)潔期間,直接將實(shí)現(xiàn)寫(xiě)在構(gòu)造函數(shù)中
- ///
- public class Test
- ...{
- public Test()
- ...{
- //
- // TODO: 在此處添加構(gòu)造函數(shù)邏輯
- //
- string ConnectionString = "Data Source=LiPu; User Id=SCOTT; Password=scott";
- //C#連接Oracle數(shù)據(jù)庫(kù)字符串,Data Source 是指數(shù)據(jù)庫(kù)名字.如我用的是本機(jī)的Oracle 的數(shù)據(jù)庫(kù),名字為L(zhǎng)iPu. user id 是
- //用戶(hù)名,你可以用System 或是你自己添加的一個(gè)用戶(hù).Password是對(duì)應(yīng)用戶(hù)的密碼.
- OracleConnection conn = new OracleConnection(ConnectionString); //創(chuàng)建一個(gè)新連接
- try
- {
- conn.Open(); //打開(kāi)連接
- OracleCommand cmd = conn.CreateCommand();
- cmd.CommandText = "select * from emp"; //SQL語(yǔ)句
- OracleDataReader rs = cmd.ExecuteReader();
- while (rs.Read()) //讀取數(shù)據(jù),如果rs.Read()返回為false的話(huà),就說(shuō)明到記錄集的尾部了
- {
- MessageBox.Show(rs.GetString(1));
- }
- rs.Close();
- }
- catch (Exception e)
- ...{
- MessageBox.Show(e.Message);
- }
- finally
- ...{
- conn.Close();
- }
- }
- }
- }
C#連接Oracle數(shù)據(jù)庫(kù)(更改數(shù)據(jù)庫(kù)中的記錄并查詢(xún)更改后的數(shù)據(jù))
- 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是手工寫(xiě)的。別的都是系統(tǒng)自動(dòng)生成的
- string constring = "data source=wzd;user=wzd;password=wzd;";//定義連接數(shù)據(jù)庫(kù)的字符串
- OracleConnection conn = new OracleConnection(constring);//進(jìn)行連接
- try
- {
- conn.Open();//打開(kāi)指定的連接
- OracleCommand com = conn.CreateCommand();
- com.CommandText = "select name from fin_ipr_inmaininfo where card_no='0000000002'";//寫(xiě)好想執(zhí)行的Sql語(yǔ)句
- OracleDataReader odr = com.ExecuteReader();
- while (odr.Read())//讀取數(shù)據(jù),如果返回為false的話(huà),就說(shuō)明到記錄集的尾部了
- {
- this.lbl.Text = odr.GetOracleString(0).ToString();//將讀取到的值顯示到定義的控件中。
- }
- odr.Close();//關(guān)閉reader.這是一定要寫(xiě)的
- }
- catch
- {
- MessageBox.Show("erro");//如果發(fā)生異常,則提示出錯(cuò)
- }
- finally
- {
- conn.Close();//關(guān)閉打開(kāi)的連接
- }
- #endregion
- }
- private void button2_Click(object sender, EventArgs e)
- {
- #region 從region到endregion是手工寫(xiě)的。別的都是系統(tǒng)自動(dòng)生成的
- string constring = "data source=wzd;user=wzd;password=wzd;";//定義連接數(shù)據(jù)庫(kù)的字符串
- OracleConnection conn = new OracleConnection(constring);//進(jìn)行連接
- try
- {
- conn.Open();//打開(kāi)指定的連接
- OracleCommand com = conn.CreateCommand();
- com.CommandText = "update fin_ipr_inmaininfo set name='wzd' where card_no='0000000002'";//寫(xiě)好想執(zhí)行的Sql語(yǔ)句
- com.ExecuteNonQuery();
- }
- catch
- {
- MessageBox.Show("erro");//如果發(fā)生異常,則提示出錯(cuò)
- }
- finally
- {
- conn.Close();//關(guān)閉打開(kāi)的連接
- }
- #endregion
- }
- }
- }
文章名稱(chēng):C#連接Oracle數(shù)據(jù)庫(kù)字符串
URL地址:http://www.5511xx.com/article/dhgdhje.html


咨詢(xún)
建站咨詢(xún)
