Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 30-Oct-12 22:36pm
v2

1 solution

Hello,

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

regards.
Tarik K.
 
Share this answer
 
Comments
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900