0

I've written an SQL query using stringbuilder and send it off to the database using SqlteDataReader as seen below:

string criteria = String.Format("SELECT tile_id FROM {0} WHERE zoom_level = 14", dbTable);
                    SQLiteCommand command = new SQLiteCommand(criteria, dbConn);
                    SQLiteDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {

                        Console.WriteLine("Starting reader: \n");
                        foreach (var i in reader)
                        {
                            Console.WriteLine(i);
                        }
                    }
              isMet = true;
                }
            catch (Exception E )
            {
                Console.Write(E.StackTrace);
            }

dbTable == map. I've ran the above SQL query through an SQLite browser and it's pulled back the correct information.

The returned value is always System.Data.Common.DataRecordInternal and after some Googling around I'm still a little lost. A lot of solutions that I've found implement a DataReader which solves the issue but I'm already using one.

tile_id is stored as TEXT in SQLite, and so far I've tried to retrieve it formatted as string, var and int, all of which have returned the above value.

Can anyone offer some advice?

2 Answers 2

2

You don't need that foreach loop. You should change the reader reading code to be

            Console.WriteLine("Starting reader: \n");
            while (reader.Read())
            {
              Console.WriteLine(reader["tile_id"].ToString());
            }

See Retrieving Data Using a DataReader. for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, thank you so much. Always nice to find shorter solutions! Marking as correct.
1

reader.Read() already moves to the next record if any. So Use:

while (reader.Read())
{
    Console.WriteLine("Starting reader: \n");
    Console.WriteLine(reader.GetString(0));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.