Wednesday, April 23, 2014

(DbConnection)Activator.CreateInstance(Type.GetType(provider), connectionString))




Hey, which ever way we go, this is a good idea…

 

From Blog Post:

==========================================================

 

Here's some code that i think is what you want.

Everything uses the system.data.common,

and the only oracle specific part is the value of the 'provider' string:

 

The generic class in the 'DbConnection conn' variable will handle

both SQL and Oracle (and more).

==========================================================

using System;

using System.Collections.Generic;

using System.Text;

 

using System.Data.Common;

 

namespace OdpTest

{

class Program

{

static void Main(string[] args)

{

string connectionString = "Data Source=(DESCRIPTION=

                                              (ADDRESS=(PROTOCOL=TCP)

                                              (HOST=MYHOST)(PORT=1527))

                                              (CONNECT_DATA=(SID=MYSERVICE)));

                                              User Id=MYUSER;Password=MYPASS;";


string provider = "Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess";

 

Console.WriteLine("Connecting");

 

using (DbConnection conn = (DbConnection)Activator.CreateInstance(Type.GetType(provider), connectionString))

{

conn.Open();

Console.WriteLine("Connected");

string sql = @"

select distinct owner

from sys.all_objects

order by owner";

Console.WriteLine("Looking for all DB owners, wait a few seconds:");

using (DbCommand comm = conn.CreateCommand())

{

comm.CommandText = sql;

using (DbDataReader rdr = comm.ExecuteReader())

{

while (rdr.Read())

{

string owner = rdr.GetString(0);

Console.WriteLine("{0}", owner);

}

}

}

}

}

}

}

 

The FindAll function returns a list of people.

This list would be usable to bind to grid-views or the like so would be almost as useful as a data-set, minus all the complication. Hope this helps.

 

public class People

{

public static List<person> FindAll()

{

string connectionString = "Data Source=(DESCRIPTION=

                                              (ADDRESS=(PROTOCOL=TCP)

                                              (HOST=MYHOST)(PORT=1527))

                                              (CONNECT_DATA=(SID=MYSERVICE)));

                                              User Id=MYUSER;Password=MYPASS;";


string provider = "Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess";

List<person> list = new List<person>();

 

using (DbConnection conn = (DbConnection)Activator.

CreateInstance(Type.GetType(provider), connectionString))

{

conn.Open();

using (DbCommand comm = conn.CreateCommand())

{

comm.CommandText = "select id,first_name,last_name from people";

using (DbDataReader rdr = comm.ExecuteReader())

{

while (rdr.Read())

{

Person p = new Person();

p.id = rdr.GetInt32(0);

p.firstName = rdr.GetString(1);

p.lastName = rdr.GetString(2);

list.Add(p);

}

}

}

}

 

return list;

}

}

 

public class Person

{

public int id { get; set; }

public string firstName { get; set; }

public string lastName { get; set; }

}

 

Source: http://splinter.com.au/using-the-new-odpnet-to-access-oracle-from-c

 

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/482a773c-c215-4483-be78-670fddfe6dae/

 

No comments:

Post a Comment