Problems:1. Missing reference workaround, 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 () + ";p wd=" +this.password.text.trim () + ";"; Globals.username = This.username.Text.Trim (); Globals.databasename = This.databaseName.Text.Trim ();
This is my consistent style, like to use the Globals class to manage common strings. Easy to manage, you can know the connection string by the code above.
3. Enquiry:
Query format as above
Running results: 1. Login:
2. Login results:
3. Display the main interface (test)
4. Query the database:
At this point, the query function is complete
Key points:
1. In the query statement, do not bring a semicolon, will prompt the keyword error
2. To add a reference:
Using System.Data.Common;
Using Oracle.dataaccess;
3. Connect the string to write well:
"Data source=shinepans;uid=system;pwd=123;unicode=true"
The above is rewritten as yours.
Code slices:
Program.cs
Using system;using system.collections.generic;using system.linq;using system.windows.forms;namespace Net3._5_ oracletest{ Static Class Program {///<summary>// The 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 button1_click (object sender, EventArgs e) {globals.connectionstring = "Data source=" +this.databasename.text.trim () + "; uid=" +this.username.text.trim () + ";p Wd= "+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 to 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 button1_click (object sender, EventArgs e) {globals.connectionstring = "Data source=" +this.databasename.text.trim () + "; uid=" +this.username.text.trim () + ";p Wd= "+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 to 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:fo RM {public Grideviews_userinfo () {InitializeComponent (); private void Grideviews_userinfo_load (object sender, EventArgs e) {String SQL1 = "SELECT * FRO M 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; } }}
Summary: If your reference does not have the two I said, you need to find a reference in the project, and then right-click to add
This is known to all, Oracle's various errors are annoying, all kinds of mistakes, fortunately there is Baidu, can query the wrong where, so don't be afraid of difficulties, more summary.
. NET connection to Oracle database---Shinepans