C# sample program for.NET Core Provider to demonstrate retrieval/RecordSet of data from OneDB Server

Open “VS2017 Command Prompt” (or later version) window. Below is the sample C# program to use OneDB .NET Core Provider.

It connects to “sysmaster” database and selects few records from “systables” table. You need to change the connection string in IfxConnection() call which suits your OneDB Server environment. This program uses .NET Core SDK v3.1.302 version.

You can use any text editor to use the below sample C# code, name it “dotnet-code-connect.cs “ and save the file. In the below image, you can refer the project file used, you need to have your own similar project file to suit your environment (i.e. location for files) and all dependent assemblies i.e. System.Security.Permissions, Microsoft.Win32.Registry etc installed (may be from www.nuget.org).

  • In the command prompt run “dotnet build”.
  • It will create the binaries for the sample program.
  • You can run the binary “dotnet <name of the DLL created in the ‘dotnet build’ phase>”
  • The program will display the data and number of records it fetched from the database on its successful execution.
  • You can refer below image for actual outputs in the environment where its executed.
using System;
using System.Data;
using Informix.Net.Core;
class TestClass
{
static void Main(string[] args)
{
IfxConnection conn = null;
int rows = 0;
String value = "";
Console.WriteLine("OneDB .NET Core Provider test");
conn = new IfxConnection("Server=onedb_6;User ID=informix;password=xxxx;Database=sysmaster;");
conn.Open();
IfxCommand Selcmd = conn.CreateCommand();
Selcmd.CommandText = "select tabname from systables where tabid<5;";
Selcmd.CommandType = CommandType.Text;
Selcmd.Connection = conn;
IfxDataReader DReader = Selcmd.ExecuteReader();
while (DReader.Read())
{
rows++;
value = DReader[0].ToString();
Console.WriteLine("Value: " + value);
}
Console.WriteLine("Number of Rows Retrieved: " + rows);
DReader.Close();
conn.Close();
conn.Dispose();
}
}

You can refer more demo/sample programs from %INFORMIXDIR%/demo folder of your OneDB Client SDK installation.