|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |
|

January 25th, 2006, 10:55 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Location: , , Spain.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 25th, 2006, 01:53 PM
|
 |
Friend of Wrox
Points: 16,368, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

January 26th, 2006, 04:45 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Location: , , Spain.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 26th, 2006, 06:13 PM
|
|
Authorized User
|
|
Join Date: Dec 2005
Location: San Nicolas, Buenos Aires, Argentina.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 26th, 2006, 07:40 PM
|
 |
Friend of Wrox
Points: 16,368, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

January 26th, 2006, 08:35 PM
|
|
Authorized User
|
|
Join Date: Dec 2005
Location: San Nicolas, Buenos Aires, Argentina.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 30th, 2006, 07:37 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Location: , , Spain.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 30th, 2006, 07:17 PM
|
|
Authorized User
|
|
Join Date: Dec 2005
Location: San Nicolas, Buenos Aires, Argentina.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 31st, 2006, 05:23 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Location: , , Spain.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks a lot, Ariel ;)
|

February 1st, 2006, 01:04 AM
|
 |
Friend of Wrox
Points: 16,368, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |