Content Management and Capture

Content Management and Capture

Come for answers. Stay for best practices. All we’re missing is you.

 View Only

IBM Datacap and PostgreSQL

By Sneha Gaddipati posted Thu November 25, 2021 04:13 AM

  
IBM Datacap out-of-box action libraries allows us to connect to databases - db2,MSAccesses,SQL Server, Oracle to export page level and field level values to database tables. If we want to export our data to PostgreSQL, since Datacap doesn't support PostgreSQL, we will have to develop an .Net custom action using NPGSQL to use in our rulesets.

NPGSQL - .NET Access to PostgreSQL:
Npgsql is an open source ADO.NET Data Provider for PostgreSQL. It allows programs written in C#, Visual Basic to access the PostgreSQL database server.

Installing NPGSQL package using NuGET Package Manager(Visual Studio):
We need to go to Projects – Manage NuGet Packages - Search for the required package (npgsql) ---click on Install


Basic code snippet to connect to Postgres using Npgsql:

// Connect to DB
NpgsqlConnection conn = new NpgsqlConnection("Server=myserver;Port=5432;User Id=muser;Password=mypassword;Database=mydatabase");
conn.Open();
// Insert query
NpgsqlCommand cmd = new NpgsqlCommand("Insert into tablename (column1,column2,column3) VALUES('" + value1 + "','" + value2 + "','" + value3 + "')", conn);

// Execute a query
int nRows = cmd.ExecuteNonQuery();
WriteLog(String.Format("Number of rows inserted={0}", nRows));

For more information related to Npgsql API, please go through this link https://www.npgsql.org/doc/api/Npgsql.html

Issues we may face while using NPGSQL:
Error:
Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
It couldn’t find 4.0.4.1 version of CompilerServices dll

Solution:

1. Install  required version of DLL:
In-order to have 4.0.4.1 assembly version of CompilerServices, we need to install 4.5.3 nuget version of CompilerServices using below command from PackageManagerConsole (Visual Studio - your project - Tools - Nuget packagemanager - PackageManagerConsole)
      paket add System.Runtime.CompilerServices.Unsafe --version 4.5.3



2.  App.config changes:
After installing the nuget package, bindingRedirect for the assembly needs to be updated to correct version.
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1"/>
</dependentAssembly>


0 comments
60 views

Permalink