dll

J

Joaquim Pinto

Hi All,

I'm making a dll that opens a conn to a database and must return all
data from a particular row by using the getstring() method.

if i only have one record everything goes well.

the problem apperas when i have more than one record.

the value returned is always the last value.

i wrote the following code:

public static string v;

OdbcConnection conn=
new OdbcConnection("STMT=;DSN=LigaMaquinas;PORT=;UID=;DB=maquinas;OPTION=0;SERVER=localhost");

string mySelectQuery = "SELECT * FROM maquinas";

OdbcCommand myCommand = new OdbcCommand(mySelectQuery,conn);
conn.Open();
OdbcDataReader myReader;
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
v=myReader.GetString(2);
}
return v;
Can anyone give an hand on this.
 
P

Peter Rilling

If I read your code correctly, you are overwriting the value of "v" for each
record. Then you are returning "v" which has only the last record's
content.
 
J

Joaquim Pinto

Yes. That's correct.

What i want is to return all records avaiable on the table and so far i
didn't find a way to do it.

How can i do that?

Regards,
 
R

Robert Hurlbut

Joaquim,

The problem has to do with how you are moving through the DataReader records
and resetting the v to the most recent record's value.

It depends on what you want to do with the strings that are returned. Let's
say you want a concatenated list of the strings, separated by a "comma". You
could do this using a StringBuilder object and appending each of the string
values into one concatenated string and returning that result:

using System.Text;

// Same code as before in your example

// Code added
StringBuilder sb;
//
while(myReader.Read())
{
// Modified code
sb.Append(myReader.GetString(2)); // Append the string
sb.Append(","); // Add a comma for the delimiter
}
sb.Remove(sb.Length, 1) // Remove the last comma
return sb.ToString(); // Return the string version of the sb object

This is just a suggestion. Again, it depends on what you are trying to
accomplish.

Robert Hurlbut
http://www.securedevelop.net
http://weblogs.asp.net/rhurlbut
 
R

Robert Hurlbut

My apologies ... some corrections to my code/example:

using System.Text;

// Same code as before in your example

// Code added
StringBuilder sb = new StringBuilder();
//
while(myReader.Read())
{
// Modified code
sb.Append(myReader.GetString(2)); // Append the string
sb.Append(","); // Add a comma for the delimiter
}
sb.Remove(sb.Length - 1, 1); // Remove the last comma
return sb.ToString(); // Return the string version of the sb object

Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top