Connecting to Oracle from Visual Studio 2010 - visual-studio-2010

I would like to connect to an Oracle 11g databse from Visual Studio 2010 using ODBC. I was not able to connec tusing ODP.NET, so I want to try using ODBC. Can someone please tell me what are the steps involved in this?

Assuming you are using C#,
You will have to add a reference to System.Data.OracleClient.dll in your project
Here is some sample boilerplate code,
using System.Data.OracleClient;
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=myserver.server.com;Persist Security Info=True;" +
"User ID=myUserID;Password=myPassword;Unicode=True";
}
// This will open the connection and query the database
static private void ConnectAndQuery()
{
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionString: {0}",
connection.ConnectionString);
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM MYTABLE";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["MYFIELD"];
Console.WriteLine(myField);
}
}
}
Source - http://www.codeproject.com/KB/database/C__Instant_Oracle.aspx

Related

ObjectContext.Translate not working in oracle 19 version

this is my function that translate result from db to object classe.
public static List<T> GetListBySP<T>(string pSPName, params object[] pParamArray)
{
List<T> res = null;
//string connStr = ConfigurationManager.ConnectionStrings[sConnStr].ConnectionString;
string connStr = DbFactory.ConnStr;
using (OracleConnection conn = new OracleConnection(connStr))
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandText = pSPName;
cmd.CommandType = CommandType.StoredProcedure;
FillParameters(cmd.Parameters, pParamArray);
cmd.Parameters.Add("pCursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
using (var db = DbFactory.CreateRo())
{
try
{
cmd.Connection.Open();
var reader = cmd.ExecuteReader();
res = ((IObjectContextAdapter)db).ObjectContext.Translate<T>(reader).ToList<T>();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Connection.Close();
}
}
}
}
return res;
}
after update oracle version to 19.
exaption throw on line
res = ((IObjectContextAdapter)db).ObjectContext.Translate<T>(reader).ToList<T>();
the exeption is:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
the project run on .net framework 4.6.2

Oracle database over ODBC converts non-english characters to question marks

I am working on retrieving data from an Oracle data source, however I am having issues with non-english characters being replaced with question marks. Some background:
I am pulling data from an Oracle view whose columns are defined as VARCHAR2
When accessing said view through Oracle SQL Developer, the results display properly
I have attempted to update my System.Web.Odbc to the latest version to no avail
I am aware that the console does not properly display certain Unicode characters, but even viewing the raw data using breakpoints shows said characters replaced with '?'
Here is a runnable example with some specific implementation details removed:
using System;
using System.Data;
using System.Data.Odbc;
namespace OdbcTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Trying ODBC Connection");
DataTable odbcData = GetData("SELECT NAME as name FROM SYSADM.[ViewName] WHERE TRUNC(sysdate) - to_date(HIRE_DT) BETWEEN 0 AND 20");
Console.WriteLine(odbcData.Rows.Count.ToString() + "rows extracted.");
foreach (DataRow dataRow in odbcData.Rows)
{
foreach (var item in dataRow.ItemArray)
{
Console.WriteLine(item);
}
}
Console.ReadKey();
}
public static DataTable GetData(string SQLStatement)
{
try
{
using (System.Data.Odbc.OdbcConnection connection = new OdbcConnection([DSN Details]))
{
DataSet ds = new DataSet();
OdbcCommand command = new OdbcCommand(SQLStatement, connection);
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
command.CommandTimeout = 600;
connection.Open();
adapter.Fill(ds);
connection.Close();
return ds.Tables[0];
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}

Searching VARCHAR2 in oracle 11g DB

I am seraching varchar2 column "DF_FORM_COMP_VALUE" that includes ID Number with address to retrieve data by searching according to the ID Number only in oracle 11g DB. I built the following code to retrieve the data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String mycon = "DATA SOURCE=mydatasource/example;USER
ID=user;Password=mypassword;Unicode=True";
String myquery = "Select * From DF_FORM_COMP_VALUE Where VALUE_STR =" +
TextBox1.Text;
OracleConnection con = new OracleConnection(mycon);
OracleCommand cmd = new OracleCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Label1.Text = "Particular ID found successfully";
Label2.Text = ds.Tables[0].Rows[0]["ID_TRANSACTION"].ToString();
Label3.Text = ds.Tables[0].Rows[0]["ID_FORM_COMPONENT"].ToString();
Label4.Text = ds.Tables[0].Rows[0]["ID"].ToString();
}
else
{
Label1.Text = "ID Not Found - Please Serach Again";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
}
con.Close();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
it keeps throwing Error ORA-01722: invalid number! Can someone help please
I'm assuming TextBox1.Text is some sort of input from the user on a web page somewhere? If the contents of that variable are "x", you're going to wind up with this query:
Select * From DF_FORM_COMP_VALUE Where VALUE_STR = x
That's not going to work because you need the string "x", but here it looks like a variable.
You probably want this line to look like:
String myquery = "Select * From DF_FORM_COMP_VALUE Where VALUE_STR = '" +
TextBox1.Text + "'";
THIS IS A VERY BAD IDEA. DO NOT DO THIS.
You need to read up on SQL Injection. Never put unsafe, user-specified text directly into a SQL query. Doing so will make your application trivially hackable.
I'm not sure what you're using to connect to Oracle, but you want to create a parameterized query and put the user input as a bind variable. One example on doing that is here. So you really want your query to look like this:
String myquery = "Select * From DF_FORM_COMP_VALUE Where VALUE_STR = :myinput"
Then you bind TextBox1.Text to :myinput, depending on what you're using to access Oracle. This is also more efficient if you run the query multiple times, because your query will not need to be hard parsed each time it is executed.

Dapper and OrmLite IDBConnectionFactory in ServiceStack

I am impressed with the speed that I can create services using ServiceStack, but for a while now I have not been able to grasp the concept of using OrmLite and Dapper simultaneously in my project. I am registering a IDbConnectionFactory like this in my Global.asax.cs
public override void Configure(Funq.Container container)
{
var dbConnectionFactory =
new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("test"), true, OracleDialect.Provider);
container.Register<IDbConnectionFactory>(dbConnectionFactory);
container.Register<ISubscriberRepository>(
c => new SubscriberRepository(c.Resolve<IDbConnectionFactory>()));
}
That works fine for OrmLite but it is a not as simple for Dapper. Maybe I am just thinking this should be more convenient than it really is. In my repository I am trying to call a Oracle stored procedure. That is my main reason for using Dapper and not OrmLite for this process. This is my repository:
public class SubscriberRepository : ISubscriberRepository {
public SubscriberRepository(IDbConnectionFactory conn) {
_conn = conn;
}
public IDbConnectionFactory _conn { get; set; }
public SubscriberResponse GetSubscriber(SubscriberRequest request) {
using (IDbConnection db = _conn.OpenDbConnection()) {
var resp = new SubscriberResponse();
List<Subscriber> s = db.Select<Subscriber>(
q => q.Subscribernum == request.Subscribernum &&
q.Personcode == request.Personcode &&
q.Clientcode == request.Clientcode);
resp.Subscriber = s[0];
return resp;
}
}
public SubscribersResponse SearchSubscribers(SubscribersRequest request) {
var response = new SubscribersResponse();
using (var cnn = new OracleConnection("this is my conneciton string")) {
cnn.Open();
var p = new OracleDynamicParameters();
p.Add("#username", "uname", OracleDbType.Varchar2);
p.Add("#Subscribernum", "", OracleDbType.Varchar2);
p.Add("#Personcode", "", OracleDbType.Varchar2);
p.Add("#Lastname", "TEST", OracleDbType.Varchar2);
p.Add("#Firstname", "HA", OracleDbType.Varchar2);
p.Add("#Mi", "", OracleDbType.Varchar2);
p.Add("#Dob", null, OracleDbType.Date);
p.Add("#MaxResults", 200, OracleDbType.Int32);
p.Add("#Curs", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output);
using (SqlMapper.GridReader multi = cnn.QueryMultiple("SEARCHSUBSCRIBER", p,
commandType: CommandType.StoredProcedure)) {
List<SearchSubscriberResults> r = multi.Read<SearchSubscriberResults>().ToList();
response.Results = r;
}
}
return response;
}
}
This works. But it isn't really using the IDbConnectionFactory at all in the SearchSubscribers function. I don't want to look at connection strings in my repository really since I could really register them all up front in the service itself.
I tried to use ServiceStack.Razor.Dapper.SqlMapper.QueryMultiple() but that doesn't work because I can't map the Oracle sys_refcursor back to anything without using the OracleDynamicParamaters workaround.
So, my question is can I create a connection for Dapper from the IDBConnectionFactory?
Thanks!
I haven't used Oracle in .NET (not since my Perl days), but OracleConnection implements the interface IDbConnection.
You should be able to cast the db connection you grab from:
IDbConnection db = _conn.OpenDbConnection()
and cast it to OracleConnection (assuming the OrmLite provider creates that same instance).
var cnn = db as OracleConnection;
... then you can try calling all that Oracle-specific stuff, like OracleDynamicParameters.
You may have to set this in AppHost.Configure() or somewhere:
OrmLiteConfig.DialectProvider = new OracleOrmLiteDialectProvider();

How do I use Oracle's ClientId property with Enterprise Library?

Just stumbled across the new ClientId (aka client_identifier) variable that is available in Oracle 10g onwards, and would love to incorporate that into an app to assist with audit trails.
The app is using a generated Enterprise Library DAAB based layer (netTiers) that is wired to use the ODP.NET drivers with EntLibContrib, so CRUD functions create an OracleDatabase object and then retrieve generic DbCommand objects from it
It looks like the OracleConnection class has the ClientId property, so what is the cleanest way to get to the Connection object within that pattern? Should I be grabbing the connection out of every DbCommand I create and setting it there, or is that overkill?
Because EntLib is doing much of the connection management behind the scenes, I'm not sure whether I can set the ClientId somewhere outside of the CRUD functions and expect it to persist?
If the connection is a partial class you can implement a trigger that sets the client id whenever the connection changes state to open.
That's the way I implemented it.
I don't know if you can use part of this:
public partial class DataContext
{
partial void OnContextCreated()
{
if ( null == this.Connection ) return;
this.Connection.StateChange += Connection_StateChange;
}
private EntityConnection EntityConnection
{
get { return this.Connection as EntityConnection; }
}
private OracleConnection OracleConnection
{
get { return this.EntityConnection.StoreConnection as OracleConnection; }
}
private void Connection_StateChange( object sender, StateChangeEventArgs e )
{
if ( e.CurrentState != ConnectionState.Open ) return;
OracleConnection conn = this.OracleConnection;
if ( null == conn ) return;
//closes connection on DataContext (bug?), and passes closed/broken connection
//conn.ClientId = HttpContext.Current == null ? "Anonymous" : HttpContext.Current.Profile.UserName;
//working solution
string identity = HttpContext.Current == null ? "Anonymous" : HttpContext.Current.Profile.UserName;
OracleCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "DBMS_SESSION.SET_IDENTIFIER";
cmd.Parameters.Add( new OracleParameter { ParameterName = "client_id", Value = identity } );
cmd.ExecuteNonQuery();
cmd.Dispose();
return;
}
protected override void Dispose( bool disposing )
{
if ( null != this.Connection )
this.Connection.StateChange -= Connection_StateChange;
base.Dispose( disposing );
}
}

Resources