. Net connection to oracle Database --- Shinepans, oracle --- shinepans

Source: Internet
Author: User
Tags oracleconnection

. Net connection to oracle Database --- Shinepans, oracle --- shinepans
FAQ: 1. the reference solution is missing. Add reference:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.OracleClient;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.Common;using Oracle.DataAccess;

2. connection string:
 globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";            globals.username = this.username.Text.Trim();            globals.databaseName = this.databaseName.Text.Trim();

This is my consistent style. I like to use the globals class to manage general strings. It is convenient to manage them. The above code can be used to know the connection string.

3. Query:
 
The Query format is as follows:

Running result: 1. Logon:


2. logon result:


3. display the main interface (test)


4. query the database:





So far, the query function has been completed


Key points:
1. do not include a semicolon in the query statement. A keyword error is prompted.
2. Add references:
Using System. Data. Common;
Using Oracle. DataAccess;
3. Write the connection string:
"Data Source = shinepans; uid = system; pwd = 123; unicode = True"

The above is changed to your


Code piece:
Program. cs
Using System; using System. collections. generic; using System. linq; using System. windows. forms; namespace Net3. _ 5_oracleTest {static class Program {// <summary> // main entry point of the application. /// </Summary> [STAThread] static void Main () {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); Application. run (new LoginForm ());}}}


LoginForm. cs
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. data. oracleClient; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace Net3. _ 5_oracleTest {public partial class LoginForm: Form {public LoginForm () {InitializeComponent ();} private void textBox1_TextChanged (object sender, EventArgs e) {} private void Button#click (object sender, EventArgs e) {globals. connectionString = "Data Source =" + this. databaseName. text. trim () + "; uid =" + this. username. text. trim () + "; pwd =" + this. password. text. trim () + ";"; globals. username = this. username. text. trim (); globals. databaseName = this. databaseName. text. trim (); OracleConnection conn = new OracleConnection (globals. connectionString); try {conn. open (); MessageBox. show ("connected Connect to the Oracle database! "); MainForm mf = new MainForm (); this. hide (); mf. show ();} catch (Exception ex) {MessageBox. show (ex. message);} finally {conn. close ();}}}}


MainForm. cs
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. data. oracleClient; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace Net3. _ 5_oracleTest {public partial class LoginForm: Form {public LoginForm () {InitializeComponent ();} private void textBox1_TextChanged (object sender, EventArgs e) {} private void Button#click (object sender, EventArgs e) {globals. connectionString = "Data Source =" + this. databaseName. text. trim () + "; uid =" + this. username. text. trim () + "; pwd =" + this. password. text. trim () + ";"; globals. username = this. username. text. trim (); globals. databaseName = this. databaseName. text. trim (); OracleConnection conn = new OracleConnection (globals. connectionString); try {conn. open (); MessageBox. show ("connected Connect to the Oracle database! "); MainForm mf = new MainForm (); this. hide (); mf. show ();} catch (Exception ex) {MessageBox. show (ex. message);} finally {conn. close ();}}}}

GridView_UserInfo.cs
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.OracleClient;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.Common;using Oracle.DataAccess;namespace Net3._5_oracleTest{    public partial class GrideViews_UserInfo : Form    {        public GrideViews_UserInfo()        {            InitializeComponent();        }        private void GrideViews_UserInfo_Load(object sender, EventArgs e)        {            string sql1 = "SELECT * FROM infoofuser";            OracleConnection myconn = new OracleConnection(globals.connectionString);            OracleCommand mycmd = myconn.CreateCommand();            mycmd.CommandText = sql1;            myconn.Open();            OracleDataAdapter oraDA = new OracleDataAdapter(mycmd);            DataSet ds = new DataSet();            oraDA.Fill(ds);            myconn.Close();            DataTable dtbl = ds.Tables[0];            this.dataGridView1.DataSource = dtbl;        }    }}


Conclusion: If your reference does not contain any of the two mentioned items, you need to find the reference in the project, right-click and choose add

This is what we all know. oracle errors are troublesome and all kinds of errors. Fortunately, Baidu can query where the errors are, so don't be afraid of difficulties.





Related Article

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.