Query DB2 from .NET

Code snippet logged for future reference.  C# to connect to DB2. With a bit of caching code added this is handy to use from within a BizTalk map.

using System;
using System.Data;
using System.Data.OleDb;
using System.Threading.Tasks;

namespace DB2Connection
{
    class Program
    {
        static void Main(string[] args)
        {
            //DB2OLEDB
            OleDbConnection connection = new OleDbConnection("Provider=DB2OLEDB;User ID=;Password=;Initial Catalog=;Network Transport Library=TCPIP;Host CCSID=1208;PC Code Page=1252;Network Address=;Network Port=;Package Collection=;Default Schema=;Units of Work=RUW;Default Qualifier=;DBMS Platform=DB2/AS400;Use Early Metadata=False;Defer Prepare=False;DateTime As Char=False;Rowset Cache Size=0;Binary CodePage=0;Datetime As Date=False;AutoCommit=True;Database Name=;Authentication=Server;Decimal As Numeric=False;Derive Parameters=True;LoadBalancing=False;Persist Security Info=True;Cache Authentication=False;Connection Pooling=False;");
            connection.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT IVAN8 FROM PYCRPDTA.F4104 where IVCITM = '9300633034444' and IVXRT = 'CE'", connection);

            DataSet ds = new DataSet();

            try
            {
                adapter.Fill(ds);
                Console.Write(ds.Tables[0].Rows[0][0]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}
Advertisements
This entry was posted in .NET. Bookmark the permalink.

5 Responses to Query DB2 from .NET

  1. mellerbeck says:

    What does the @ signify here: new OleDbDataAdapter(@”SEL
    I’ve been trying to get it to work in VB.net but keeps throwing a

    An unexpected token was found. Expected tokens may include: . SQLSTATE: 42601, SQLCODE: -104

  2. mellerbeck says:

    Thank you, thank you! This was the clue I needed. Since vb.net doesn’t have a verbatim string literal I had to use this XML hack, Dim cmdText as String = .Value

    Once I did that it started working!

  3. Pingback: How to actually get the AS400 oledb connection with .net | Michael Ellerbeck

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s