Click here to Skip to main content
12,302,258 members (56,478 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hello,
please help me in Csharp for calling a method to retrieve a database value using IBM DB2

//------------------------------
public void CreateMyDB2DataReader(string mySelectQuery,string myConnectionString)
{
DB2Connection myConnection = new DB2Connection(myConnectionString);
DB2Command myCommand = new DB2Command(mySelectQuery, myConnection);
myCommand.Connection.Open();
DB2DataReader myReader = myCommand.ExecuteScalar();
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}
finally
{
myReader.Close();
myConnection.Close();
}
}
//--------------------

public static void Main(tring[] args)
{
// HOW TO CALL CreateMyDB2DataReader?
// my connection string is: "Database=Mydata;UID=login;PWD=passwd;";
// the query: "select bdate from mytable where item = 'startdate'";

}
//---------------------------------------------

Thank you very much.
Wajim
Posted 30-Oct-12 18:58pm
Edited 30-Oct-12 22:36pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

Your solution is very simple Smile | :) just use ExecuteScalar(..) method intead of the ExecuteReader(..) method.

regards.
Tarik K.
  Permalink  
Comments
Member 9326300 31-Oct-12 4:38am
   
Hi,
thanks for reply.
I have replaced ExecuteReader by ExecuteScalar, I know it is better for retrieving 1 single value. But my question is How do I use it in the MAIN method?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160530.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100