Pervasive SQL and .NET Part 2: PSQL ADO.NET Provider
Sunday, October 11, 2009 at 11:00PM In Part 1, I showed you how to access a Pervasive SQL database using ODBC. You can also do this using Pervasive's ADO.NET Provider which bypasses ODBC entirely.
PSQL ADO.NET Provider
Unlike the ODBC method, you might have to do a little bit of extra configuration. If you're running Pervasive SQL v9.5, you have to download the free Pervasive .NET SDK. If you're using v10+, then the ADO.NET provider was installed for you alongside PSQL (handy, eh?). If you want Visual Studio integration, however, you still need to download the Pervasive .NET SDK.
You can find the appropriate SDK for your version of PSQL at the Pervasive Developer Center.
Once you have all the necessary pieces, let's put it together with some code. The structure is just like the code in the first part, except the connection string is slightly different and the class names have changed:
string connString = "ServerDSN=DEMODATA";
using (PsqlConnection conn = new PsqlConnection(connString)) {
conn.Open();
// query code goes here
}
You'll notice that we're not specifying a Driver parameter in the connection string. You can use the common parameters I listed in Part 1, except DSN and Driver, which are specific to ODBC. Also, like in Part 1, we can leave out the ServerName parameter, since the engine is running locally.
The rest is nearly identical to the ODBC code, except we're using PsqlCommand and PsqlParameter. Note that parameters still aren't named in the ADO.NET Provider, so we have to use a question mark.
PsqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select ID from Person where First_Name = ?";
Console.Write("Enter First_Name: ");
string firstName = Console.ReadLine();
cmd.Parameters.Add(new PsqlParameter("?", firstName));
Console.WriteLine(cmd.ExecuteScalar());
That's all there is to it! In Part 3, I'll show you how to setup the Pervasive SQL Storage Provider included with the OPF3 ORM Framework.
.net,
c#,
pervasive sql
Reader Comments