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 27 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
In the Solution Explorer, right-click References and then select Add Reference.
2
In the Reference Manager wizard, click the Browse button and navigate to the folder that contains the PSQL data provider assembly.
3
Select Pervasive.Data.SqlClient.dll and click Add. The Browse tab of the Reference Manager wizard lists the PSQL data provider assembly in the Recent items.
4
Select it and click OK. The Solution Explorer now includes the PSQL data provider.
5
// Access PSQL
using System.Data;
using System.Data.Common;
using Pervasive.Data.SqlClient;
6
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);
}
7
// Close the connection
DBConn.Close();
Example: Using the Common Programming Model
The following example illustrates connecting to a PSQL database from an application developed in Visual Studio using C# and the Common Programming Model.
1
// Access PSQL 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 PSQL Common Assembly
You can optionally include the PSQL Common Assembly if you want to use features such as PSQL Bulk Load in an application that conforms to the Common Programming Model. See Using PSQL Bulk Load for information about how to use PSQL Bulk Load with your application.
The following example illustrates how to use the PSQL Common Assembly in an application developed in Visual Studio using C# and the Common Programming Model.
1
// Access PSQL using factory
using System.Data;
using System.Data.Common;
using Pervasive.Data.Common;
2
// This code does a bulk copy operation from one
// 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