Hi everyone,
Even though this topic is not directly connected with SharePoint, you may need or one day you will need :) to connect to the Oracle DB. Although it is not difficult task, this sample could help you to avoid some hassle with data mining from Oracle DB. (It helped me :) ) I am sure there are other approaches to do so and as always if you now better one I will be glad to know them :)
So lets start with sample. Basically you will need just one class which encapsulate whole connection and returns some data type you may need.
class OracleConnectionClass
{
private OracleConnection _conn;
#region PossibleErrors
// Some possible errors
// System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
// If this is encountered it could be a permission problem accessing the location
// where the oracle dlls are…but most likely the dlls are not in the same directory as the executable or found within the environment path of the system.
// ORA-12541: TNS:no listener
// This one could mean that one of the connection settings is incorrect and the database could not be connected to
// and this generic error comes back. Try tweaking the settings, port, instance
#endregion
#region STATIC
/// <summary>
/// creates connection to DB
/// </summary>
/// <returns>ElanorADOracleConnection to GLOBUSAD</returns>
public static OracleConnectionClass GetTestDBconnection()
{
string connectionString = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = SERVER_URL)(PORT = PORT_NUMBER))(CONNECT_DATA = (SID = YOUR_SID)));User Id=USER_NAME;Password=USER_PASSWORD;";
return new OracleConnectionClass(connectionString);
}
#endregion
#region CONSTRUCTORS
/// <summary>
/// Creates and open connection
/// </summary>
/// <param name="connString"></param>
public OracleConnectionClass(string connString)
{
try
{
_conn = new OracleConnection(connString);
_conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
#endregion
/// <summary>
/// Gets state of connection
/// </summary>
public ConnectionState State
{
get
{
return this._conn.State;
}
}
/// <summary>
/// Closes and disposes the connection
/// </summary>
public void Close()
{
if (_conn != null)
{
_conn.Close();
_conn.Dispose();
_conn = null;
}
}
/// <summary>
/// gets list of Int32 values
/// </summary>
/// <param name="SQLString">sql query string</param>
/// <returns>ArrayList of string's Int32 values </returns>
public ArrayList GetInt32ValuesArray(string SQLString)
{
ArrayList a = new ArrayList();
OracleCommand cmd = new OracleCommand(SQLString, this._conn);
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
a.Add(Convert.ToString(reader.GetInt32(0)));
}
reader.Close();
reader.Dispose();
cmd.Dispose();
return a;
}
/// <summary>
/// gets string value from db
/// </summary>
/// <param name="sql">sql query string</param>
/// <returns>returns string value of the first column of the first row</returns>
public string GetValue(string sql)
{
string retValue;
OracleCommand command = new OracleCommand(sql, this._conn);
try
{
retValue = command.ExecuteScalar().ToString();
}
catch (NullReferenceException)
{
retValue = String.Empty;
}
command.Dispose();
return retValue;
}
/// <summary>
/// gets data reader
/// </summary>
/// <param name="sql">sql query string</param>
/// <returns>OracleDataReader </returns>
public OracleDataReader GetDataReader(string sql)
{
OracleCommand cmd = new OracleCommand(sql, this._conn);
return cmd.ExecuteReader();
}
/// <summary>
/// gets disconnected data table
/// </summary>
/// <param name="sql">sql query string</param>
/// <returns>disconected DataTable</returns>
public DataTable GetTable(string sql)
{
OracleCommand command = new OracleCommand(sql, this._conn);
OracleDataAdapter da = new OracleDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = command;
da.Fill(dt);
da.Dispose();
da = null;
return dt;
}
}
I guess that code is quite understandable. Important thing is to set correct connection in "GetTestDBconnection" function. You can also create more functions to get data from various databases. The rest can remain in peace if you are willing to :)
And below is the way how to use this class to actually get some data:
class Program
{
static void Main(string[] args)
{
//Create variable
OracleConnectionClass oracleConnection = null;
//Initialize it
oracleConnection = OracleConnectionClass.GetTestDBconnection();
//Create datatable
DataTable oracleDataTable;
//Get data from DB
oracleDataTable = oracleConnection.GetTable("SELECT * FROM OracleDB");
//Close connection
oracleConnection.Close();
}
}
Ok then, that should be all. If you would find anything unclear or wrong, do not hesitate to let me know ;)
Happy codding.
No comments:
Post a Comment