A
A
Andrew10000002015-02-22 00:41:44
SQLite
Andrew1000000, 2015-02-22 00:41:44

Oddities in the work of SQLite in C #: why is the query with a parameter not executed correctly?

Here is an example of code that does not execute correctly

cmd = conn.CreateCommand();
cmd.CommandText = "SELECT @s FROM Cars";
cmd.Parameters.Add("@s", DbType.String).Value = "Id";
r = cmd.ExecuteReader();
while (r.Read())
{
    MessageBox.Show(r.GetValue(0).ToString());
    //MessageBox.Show(r["Id"].ToString());
}
r.Close();

The query returns the string "Id" five times (according to the number of records in the table), and should obviously output:
1
2
3
4
5

If you uncomment the second line, an IndexOutOfBoundsException error pops up.
What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2015-02-22
@Sumor

It will not work with SQL parameters to set the names of the columns in SQL.
At you as a result the request
which and was executed turned out. As you asked: SQL selected records from the Cars table (5 records) and returned the string 'id' for each as you requested.
There is no id column in the query. There is an unnamed column. Therefore, an attempt to return a column that does not exist results in an IndexOutOfBoundsException.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question