C # Connect Oracle database Strings "51cto Online classic books"

Source: Internet
Author: User
Tags erro oracleconnection

C # Connect Oracle database Strings

"Turn" http://developer.51cto.com/art/200908/145365.htm

C # Connect Oracle database Strings (query data)

--------------------------------------------------------------------------------------------------------------- ---------------------------------using system;   using system.collections.generic;   Using system.componentmodel   using system.data.oracleclient;;/ /This line and the next line must first fill in the reference 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   from region to Endregion is hand-written. Everything else is automatically generated by the system               string constring  =  "data source=wzd;user=wzd;password=wzd;"; /defines a string for the connection database               oracleconnection  conn = new oracleconnection (constring);//Connect                try               {                   conn. Open ();//Opens the specified connection           &NBSp;       oraclecommand com = conn. CreateCommand ();                   com. commandtext =  "select name from mytable where card_no= ' 0000000002 '";// Write the SQL statements you want to execute                    oracledatareader odr = com. ExecuteReader ();                   while   (ODR. Read ())//reads the data, if returned to false, it shows the tail of the recordset                      {                       this.lbl.text = odr. Getoraclestring (0). ToString ();//Displays the read value to the defined control.                    }                    odr. Close ();//Turn off reader. This is a must write                              catch               {                   messagebox.show ("Erro");//If an exception occurs, prompt for error                }                finally              {                   conn. Close ()//Turn off open connections              &NBSP}                   #endregion           }       &NBSP}  }

--------------------------------------------------------------------------------------------------------------- ---------------------------------

C # code to connect Oracle database strings

--------------------------------------------------------------------------------------------------------------- ---------------------------------

C # Connect to an Oracle database (change records in a database and query for changed data)

usingSystem;usingSystem.Collections.Generic;usingsystem.componentmodel;//this line and the next line are first filled in with the reference system.data.oracleclientusingSystem.Data.OracleClient;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsApplication1 { PublicPartialclassForm1:form { PublicForm1 () {InitializeComponent (); }PrivatevoidButton1_Click (Objectsender, EventArgs e) {#region from region to Endregion are written by hand. Everything else is automatically generated by the system.stringconstring = "Data source=wzd;user=wzd;password=wzd;";/ /defines the connection database string OracleConnection conn =NewOracleConnection (constring);//ConnectTry{Conn. Open ();//Opens the specified connection oraclecommand COM = conn.                   CreateCommand (); Com. CommandText = "SELECT name from Fin_ipr_inmaininfo where card_no= ' 0000000002";//write the SQL statement you want to execute Oracledatar Eader ODR = com. ExecuteReader (); while(ODR. Read ())//reads the data, if returned to false, it shows the end of the recordset { This. LbL. Text = ODR. Getoraclestring (0).                   ToString ();//Displays the read value to the defined control. } ODR. Close ();//Turn off reader. This must be written.Catch{MessageBox.Show ("Erro");//If an exception occurs, an error is prompted}finally{Conn. Close ();//Closes Open connection} #endregion}Private voidButton2_Click (Objectsender, EventArgs e) {#region from region to Endregion are written by hand. Everything else is automatically generated by the system.stringconstring = "Data source=wzd;user=wzd;password=wzd;";/ /defines the connection database string OracleConnection conn =NewOracleConnection (constring);//ConnectTry{Conn. Open ();//Opens the specified connection oraclecommand COM = conn.                   CreateCommand (); Com. CommandText = "Update fin_ipr_inmaininfo set name= ' Wzd ' where card_no= ' 0000000002 '";//write the SQL statement you want to execute COM.                 ExecuteNonQuery (); }Catch{MessageBox.Show ("Erro");//If an exception occurs, an error is prompted}finally{Conn. Close ();//Closes Open connection} #endregion}}}

--------------------------------------------------------------------------------------------------------------- ---------------------------------

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Note: Be sure to add this:

Project-> Add Reference. Net->system.data.oracleclient.dll

using system;   using system.data;   using system.windows.forms;   using system.data.oracleclient;     Namespace test   ... {      /**//**//**////        ///  Concise period, write implementation directly in constructor       ///        public class test         {          public test ()             {              //               // TODO:  Add constructor logic here                //                 string ConnectionString =  "Data source=lipu; user&nbsP;id=scott; password=scott ";    //c# Connection Oracle database string,data source  refers to the database name. If I use the database of the local oracle , the name is lipu. user  id  is    /username, you can use system  or a user you add yourself. Password is the password for the corresponding user.                     Oracleconnection conn = new oracleconnection (ConnectionString);     // Create a new connection                               try               {                    conn. Open ();     //Opening connection                     oraclecommand cmd = conn. CreateCommand ();                       cmd.commandtext =  "select * from emp";     //sql statement                     oracledatareader  rs = cmd. ExecuteReader ();                      while  (Rs. Read ())     //reads the data if Rs. If Read () returns to false, it shows the tail of the recordset          {                         MessageBox.Show (Rs. GetString (1));                    }                   &NBSp;  rs. Close ();                }                  catch  (exception e)                 {                    MessageBox.Show (E.message);                }                finally                 {                    Conn. Close ();                }            &NBSP}         }  } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.