Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2006, 10:55 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default oracle data reader

hi to everybody! i'm trying to read the values stored in my oracle database. i've been using the oracle data reader, but i can only read the first row. this is what i do:

private string Read_DB(string myConn)
{
string mySelectQuery = "SELECT COL1 FROM MYTABLE";
OracleConnection myConnection = new OracleConnection(myConn);
OracleCommand myCommand =
              new OracleCommand(mySelectQuery,myConnection);
myConnection.Open();

OracleDataReader myReader = myCommand.ExecuteReader();
try
{
OracleString name;
object date1;

while(myReader.Read())
{
  name = myReader.GetString(0);
  date1 = myReader.GetDateTime(1);
}
return name.ToString()+dat1.ToString();
}
catch(OracleException e)
{
   return e.Code + e.Message;
}
finally
{
   myReader.Close();
   myConnection.Close();
}

}

Working like this gives a compilation error: use of the local variable not assigned 'name'. could somebody help me??? thank so much!

                                          silvi



 
Old January 25th, 2006, 01:53 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Consider the following code:
Code:
OracleString name;
object date1;

while(myReader.Read())
{
  name = myReader.GetString(0);
  date1 = myReader.GetDateTime(1);
}    
return name.ToString()+dat1.ToString();
You declare 2 variables but never assign them. The only place you assign them is inside a conditional statement. You could potentially never enter that condition and thus will have unassigned variables. The compiler recognizes this and fails on it.

The easiest resolution is to set default values in the declaration:

OracleString name = string.Empty;
object date1 = null;

-Peter
 
Old January 26th, 2006, 04:45 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank your peter! i've probed my code now and i have realized that i only read the first row in my table. how can i go into next row? thanks again!

                                         silvi

 
Old January 26th, 2006, 06:13 PM
Authorized User
 
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arielote Send a message via MSN to arielote Send a message via Yahoo to arielote
Default

Silvi:
El metodo Read() retorna TRUE siempre y cuando haya lineas para leer. Creo que tu deberias hacer algo dentro del bucle condicional (WHILE) tal como llenar un array, para que cuando no haya mas lineas para leer salga del bucle condicional y regrese algun resultado como el numero de lineas leidas/

Saludos

Ariel



---
Ariel Folonier
San Nicolas
Argentina
 
Old January 26th, 2006, 07:40 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm not fluent in spanish so I can't provide a translation, but I think what Ariel is saying is:

The .Read() method call in the while will cycle you thru all the records. What you are actually ending up with is the values from the LAST record, not the first because you are looping.

If you want to do something with all the values from all the rows you'll need some repeating control to display all the results or you need to explicitly do something with the data from each iteration of the loop.

-Peter
 
Old January 26th, 2006, 08:35 PM
Authorized User
 
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arielote Send a message via MSN to arielote Send a message via Yahoo to arielote
Default

Dear Peter:
I wrote in spanish because I saw that Silvi is from Spain.
Your translation is 100% accurate. Thank you very much for that
Following posting will be written in span-glish

Regards

Ariel

---
Ariel Folonier
San Nicolas
Argentina
 
Old January 30th, 2006, 07:37 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you for your help ariel and peter. by the way i will ask another question :). i'm trying to insert a value in my table that is defined as type "number"in my database.

the next query works:

string myInsertQuery = "INSERT INTO PROYECTO (Column1) VALUES (22)";

but if a define a variable, assigne it this value and try to do the insert, it generate an error of the type: column not allowed here.
this is what i'm trying to do:

int mydata = 22;
string myInsertQuery = "INSERT INTO PROYECTO (Column1) VALUES (mydata)";

i need to use variable values, so i can't write directly a number. do you know how this sentence doesn't work?

thank you very much. i haven't ever work with databases, and i'm really lost

                                  silvi


 
Old January 30th, 2006, 07:17 PM
Authorized User
 
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arielote Send a message via MSN to arielote Send a message via Yahoo to arielote
Default

Dear Silvi:

I do not so much about ORACLE, but in SQL SERVER you've got the aggregate function CONVERT.
This means that your "variable" will be an string, in order to allow you to insert into main SQL string phrase.

I would suggest you to read you ORACLE book in order to find out equivalent funciont like CONVERT in SQL Server

Regards

Ariel


---
Ariel Folonier
San Nicolas
Argentina
 
Old January 31st, 2006, 05:23 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks a lot, Ariel ;)

 
Old February 1st, 2006, 01:04 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by silvia
 int mydata = 22;
string myInsertQuery = "INSERT INTO PROYECTO (Column1) VALUES (mydata)";
The database doesn't know what "mydata" is. You are putting a variable name directly into the literal text of a query. You need to concatenate the value into the query. I usually set up the query structure using the .NET formatting replacement tokens, then use the string.Format method for plug in the values:

int mydata = 22;
string myInsertQuery = "INSERT INTO PROYECTO(Column1) VALUES({0})";
myInsertQuery = string.Format(myInsertQuery, mydata);

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net reader with Oracle table function return type l.wolf Oracle ASP 1 July 24th, 2007 06:45 AM
Data reader checking.... janees ASP.NET 1.0 and 1.1 Professional 1 March 11th, 2007 06:09 AM
.Net reader with Oracle table function return type l.wolf ASP.NET 2.0 Basics 0 November 18th, 2006 12:36 PM
Fill data set from data reader sunil menghani ADO.NET 3 March 29th, 2005 07:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.