Establishing a connection is divided into six steps:
1. Define the connection string, and the connection string for Oracle is:
private static string connstring = "Data source=192.168. 1.130:1521/mydata; Persist Security info=true; User id=em_test; password=test123; Unicode=true ";
2. Connect based on connection string
3. Open the Connection
4. Instance Command object
5. Operation
6. Close the connection
See an example:
Using System; Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Data.OracleClient;
Namespace Openclosedb
{
public partial class Form1:form
{
Public Form1 ()
{InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
OracleConnection connection = null;
intnum =0;
String message = "";
String sql = "SELECT COUNT (*) from Ex_user";
Try
{
String connstring = "Data source=mydata; Persist Security info=true; User id=test; password=huikk123; Unicode=true ";
Create a Connection object
Connection = new OracleConnection (connstring);
Open connection
Connection. Open ();
OracleCommand command = new OracleCommand (sql, connection);
num =int. Parse (command. ExecuteScalar (). ToString ());
Message = string. Format ("Total {0} Bar User information! ", num);
MessageBox.Show (message);
}
catch (Exception ex)
{
MessageBox.Show ("Exception occurred" +ex. Message);
}
Finally
{
if (connection! = NULL)
{
Connection. Close ();
}
}
}
}
}