1 Quick Start : Connecting to a Database

Connecting to a Database
Once the data provider is installed, you can connect from your application to your database with a connection string. See the appropriate data provider chapter for connection string options.
Example A: Using the Provider-Specific Objects
The following example uses the provider-specific objects to connect to a database using the Oracle data provider from an application developed in Visual Studio using C#.
1
In the Solution Explorer, right-click References, and then select Add Reference. The Add Reference dialog box appears.
The Add Reference dialog box is opened to the .NET tab. It lists all available references.
2
The Solution Explorer now includes DDTek.Oracle, the assembly name of the Oracle data provider.
Solution Explorer window with the Oracle data provider added.
3
// Access Oracle
using System.Data;
using DDTek.Oracle;
4
OracleConnection DBConn = new OracleConnection("Host=Accounting1;Port=1521;User ID=scott;Password=tiger;Service Name=sales.us.acme.com");
try
{
   DBConn.Open();
   Console.WriteLine ("Connection successful!");
}
 
// Display any exceptions
catch (OracleException ex)
{
   // Connection failed
   Console.WriteLine(ex.Message);
   return;
}
5
// Close the connection
Conn.Close();
Example B: Using the Common Programming Model
The following example illustrates connecting to an Oracle database from an application developed in Visual Studio using C# and the Common Programming Model.
1
// Access Oracle using factory
using System.Data;
using System.Data.Common;
2
DbProviderFactory factory=DbProviderFactories.GetFactory("DDTek.Oracle");
DbConnection Conn = factory.createConnection();
Conn.ConnectionString = "host=Accounting1;Port=1521;Service Name=test;User ID=test01; Password=test01";
 
try
{
   Conn.Open();
   Console.WriteLine("Connection successful!");
}
catch (Exception ex)
{
   // Connection failed
   Console.WriteLine(ex.Message);
}
// Close the connection
Conn.Close();
Example C: Using the DataDirect Common Assembly
You can optionally include the DataDirect Common Assembly if you want to use features such as DataDirect Bulk Load in an application that conforms to the Common Programming Model. See “Using DataDirect Bulk Load” for information about how to use DataDirect Bulk Load with your application.
The following example illustrates how to use the DataDirect Common Programming Assembly in an application developed in Visual Studio using C# and the Common Programming Model.
1
// Access Oracle using factory
using System.Data;
using System.Data.Common;
using DDTek.Data.Common;
2
// This code does a bulk copy operation from one
// Oracle database to another
 
DbProviderFactory factory = DbProviderFactories.GetFactory("DDTek.Oracle")
DbConnection Conn1 = factory.CreateConnection();
Conn1.ConnectionString = "... Oracle Server ....";
Conn1.Open();
DbCommand command = "SELECT ProductID, Name FROM Products";
DbDataReader reader = command.ExecuteReader();
using (DbConnection Conn2 = factory.CreateConnection())
Conn2.ConnectionString = " ... Oracle Server ....";
Conn2.Open();
 
using (DbBulkCopy bulkCopy = new DbBulkCopy(Conn2) {
   bulkCopy.DestinationTable = "ProductsMirror";
 
      try {
         bulkCopy.WriteToServer(reader);
      }
      catch {
         Console.WriteLine(ex.Message);
      }
}