. 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.