Click to See Complete Forum and Search --> : getting all rows with ExecuteReader


Pr!nce0f4Mb3r
07-19-2006, 04:38 PM
new to .NET
playing around with database usage and taking a basic OleDB example and modifying it to see what I can do with it.

I'm using OleDDbDataReader and ExecuteReader
The change to the basic examples is that I
want on a command line to have user enter any sql statement
and I read it in as a string.

connection is straightforward
OleDBCommand cm = new OleDBCommand(cmString, cn);
OleDbDataReader = QReader
QReader = cm.ExecuteReader

and I use
while (QReader.Read()
{
Console.WriteLine(QReader.GetString(0));
}

If I use sql statements with one select argument things seem to work
select firstname from customers

If I use select * from
I get an error. Seems it doesn't like *

If I use select firstname, lastname, etc...
it will grab and write firstname, but not the rest I specify.

I'm guessing that I need GetString(1) (2) etc..., but I won't know what the
sql statement would be beforehand.

What am I missing?

Thanks

Sync
07-19-2006, 11:53 PM
Console.WriteLine(QReader.GetString[0]); // NOT (0)

One more thing is that.. it's depend on the datatype of the column. If the datatype of the column is Integer then you should use "QReader.GetInt32[0];" instead of GetString[0].

If you stilll hav prob, plz attach the sample code and database in this thread.. we will check for you..

Pr!nce0f4Mb3r
07-20-2006, 10:31 AM
hmm...

The question then becomes how you can parse the original user query to get the rows and columns
I guess you can use any database... Northwind seems to be valid here too.

not much more code....

string ConnectionString = @"Priver=Microsoft.JET.OLEDB.4.0; DataSource=<path to your data base>;
string cmString;
OleDBConnection cn = null;


Console.Write("input query ");
cmString = Console.ReadLine();

cn = new OleDBConnection(connectionString);
cn.Open();

OleDBCommand cm = new OleDBCommand(cmString, cn);
OleDbDataReader = QReader;
QReader = cm.ExecuteReader();

while (QReader.Read()
{
Console.WriteLine(QReader.GetString(0));
}

}


what else is needed to then parse through the statement to get the necessary fields and types since each column may be a different datatype?

Wonder if GetSchemaTable needs to be worked in here somehow.

Thanks for any input