Link to home
Create AccountLog in
Avatar of pclarke7
pclarke7

asked on

Returning values to variables from a c# SQL statement

Hello,
I have a C# application which uses an ODBC connection to access data from a DB2 database. Typical SQL statement(s) would be

"Select  field2,  field4,  field10   from   ItemMaster where field1 = '12345678' "

I would like to be able to return these 3 file values  (field2, field4 & field10) to variables defined within the application. Appreciate any pointers as to how I might achieve this.

regards
Pat
Avatar of SStory
SStory
Flag of United States of America image

Do you mean you execute a SQLCommand to get a dataset or datareader from DB2 and you want to get the vaues of field2,4, and 10 into C# variables?

If so:

I assume you are doing something like this:
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.dndp.doc/htm/frlrfIBMDataDB2DB2DataReaderClassTopic.htm
So take a look at this section:

 myReader = myCommand.ExecuteReader();
   // Always call Read before accessing data.
   while (myReader.Read()) {
      Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));

Open in new window


You get the data via:
myReader.GetSomething(Indexvalue--zero based)

Example:
Int32 i = myReader.GetInt32(0)

This would get the first field into an int treating it as such or cause an error if field 0 is a string.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer