So far we have written applications to talk to various Databases. This is the first time, wrote a C# .Net console application to fetch records from DB2. Following is the sample code.
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
class Program
{
static void Main()
{
OdbcCommand cmd = null;
OdbcConnection con = null;
OdbcDataReader rdr = null;
try
{
con = new OdbcConnection("DSN={LibName};UID={User Name};PWD={Password};" + "Driver={IBM DB2 ODBC DRIVER};");
cmd = new OdbcCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM SOMETABLE";
cmd.comCommandTimeout = 20;
con.Open();
rdr = cmd.ExecuteReader(System.Data.CommandBehavior.SingleResult);
int intCount = 0;
while (rdr.Read())
{
intCount++;
Console.WriteLine(rdr.GetString(2).ToString() + ", " + rdr.GetString(3) + " ->" + intCount.ToString());
}
con.Close();
Console.ReadLine();
}
catch (OdbcException myException)
{
}
}
}
Regards
Fauzi
No comments:
Post a Comment