Connecting to a Database
Once your data provider is installed, you can connect from your application to your database with a connection string. See Table 31 for a list of the connection string options.
NOTE: If your application uses the ADO.NET Entity Framework, you can use the Entity Data Model Wizard to create a new connection or use an existing connection. See Creating a Model for more information.
Example: Using the Provider-Specific Objects
The following example uses the provider-specific objects to connect to a database using the ADO.NET data provider from an application developed in Visual Studio using C#.
1
2
Click Add Reference; then, select the Pervasive PSQL data provider in the component list. If the data provider is not in the component list, click the Browse tab and navigate to the data provider assembly.
3
Click OK. The Solution Explorer now includes the Pervasive PSQL data provider.
4
// Access Pervasive PSQL
using System.Data;
using System.Data.Common;
using Pervasive.Data.SqlClient;
5
PsqlConnection DBConn = new PsqlConnection("Server DSN=DEMODATA;Host=localhost");
try
{
// Open the connection
DBConn.Open();
Console.WriteLine("Connection Successful!")
}
catch (PsqlException ex)
{
// Connection failed
writer.WriteLine(ex.Message);
}
6
// Close the connection
DBConn.Close();
Example: Using the Common Programming Model
The following example illustrates connecting to a Pervasive PSQL database from an application developed in Visual Studio using C# and the Common Programming Model.
1
// Access Pervasive using factory
using System.Data;
using System.Data.Common;
2
DbProviderFactory
factory=DbProviderFactories("Pervasive.Data.SqlClient");
DbConnection Conn = factory.createConnection();
Conn.CommandText = "Server DSN=DEMODATA;Host=localhost;";
try
{
Conn.Open();
Console.WriteLine("Connection successful!");
}
catch (Exception ex)
{
// Connection failed
Console.WriteLine(ex.Message);
}
// Close the connection
Conn.Close();
Example: Using the Pervasive Common Assembly
You can optionally include the Pervasive Common Assembly if you want to use features such as Pervasive Bulk Load in an application that conforms to the Common Programming Model. See Using Pervasive Bulk Load for information about how to use Pervasive Bulk Load with your application.
The following example illustrates how to use the Pervasive Common Assembly in an application developed in Visual Studio using C# and the Common Programming Model.
1
// Access Pervasive PSQL using factory
using System.Data;
using System.Data.Common;
using Pervasive.Data.Common;
2
// This code does a bulk copy operation from one
// Pervasive PSQL database to another
DbProviderFactory Factory = DbProviderFactories.GetFactory("Pervasive.Data.SqlClient");
DbConnection sourceConnection = Factory.CreateConnection();
sourceConnection.ConnectionString = "Host=localhost;Server DSN=DEMODATA;";
 
sourceConnection.Open();
 
DbCommand command = sourceConnection.CreateCommand();
command.CommandText = "SELECT * FROM test";
DbDataReader reader = command.ExecuteReader();
 
DbConnection destinationConnection = Factory.CreateConnection();
destinationConnection.ConnectionString =
"Host= ntsl2003b;Server DSN=DEMODATA";
destinationConnection.Open();
 
DbBulkCopy bulkCopy = new DbBulkCopy(destinationConnection);
bulkCopy.DestinationTableName = "test";
try
{
bulkCopy.WriteToServer(reader);
}//end try
catch (DbException ex)
{
Console.WriteLine( ex.Message );
}//end catch
finally
{
reader.Close();
MessageBox.Show("done");
}//end finally