0

Why my query does not return results? I'm using C#. It returns column headers but no rows. Is there a problem with my select statement?

here's my code :

conn = new SQLiteConnection("Data Source=local.db;Version=3;New=False;Compress=True;");
DataTable data = new DataTable();
SQLiteDataReader reader;
using (SQLiteTransaction trans = conn.BeginTransaction())
{
    using (SQLiteCommand mycommand = new SQLiteCommand(conn))
    {
        mycommand.CommandText = "SELECT * FROM TAGTABLE WHERE TAG = '"+tag+"' ;";
        reader = mycommand.ExecuteReader();
    }
    trans.Commit();
    data.Load(reader);
    reader.Close();
    reader.Dispose();
    trans.Dispose();
}
return data;

The TAGTABLE has following fields:

TID int,
Tag varchar(500),
FilePath varchar(1000)
7
  • Did you try to execute exactly same questy into SQLite designer of VS. ? Commented Sep 2, 2011 at 10:53
  • yes it worked,but in my program its not returning rows. Commented Sep 2, 2011 at 10:55
  • Did you debug the application? what is happening? Commented Sep 2, 2011 at 10:56
  • @armin I suspect the transaction is doing something funky to the entire command, I've posted some amended code that should work as expected. I can't answer what the trans.Commit line would do, I've no idea. Commented Sep 2, 2011 at 10:56
  • @armin There might be something funny with the column names, can you list the column names being returned and also some sample data? Also, can you provide a sample value of what tag might contain. Commented Sep 2, 2011 at 11:05

2 Answers 2

2

You don't need the transaction, try the following:

    DataTable data = new DataTable();

    using (SQLiteConnection conn = new SQLiteConnection("Data Source=local.db;Version=3;New=False;Compress=True;"))
    using (SQLiteCommand mycommand = new SQLiteCommand(conn))
    {
        mycommand.CommandText = "SELECT * FROM TAGTABLE WHERE TAG = @tag;";
        mycommand.Parameters.AddWithValue("@tag", tag);
        conn.Open();

        using (SQLiteDataReader reader = mycommand.ExecuteReader())
        {
            data.Load(reader);  
        }
    }

    return data;

The most likely reason this won't return anything is if the SELECT doesn't yield any results.

Also note that anything implementing the IDisposable interface can be used in conjunction with the using statement, so manual closing / disposal of objects afterwards is not required.

Notice that the SQL has changed to use a parameterized query, this will help reduce the likelihood of SQL injection attacks, and is generally cleaner.

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

3 Comments

Still not working,only returning columns not rows.by the way I'm using system.data.sqlite wrapper.
@armin What are the column names of the columns being returned?
Columns : TID int,Tag varchar(500),FilePath varchar(1000)
0

Since you don't show sample data and what should be returend some general pointers only:

  • The way you build the SQL is wide open to SQL incjection (a serious security issue)
  • Depending on the value of tag (for example if it contains ') the above SQL Statement would do something you don't expect
  • Since everything is wrappend in using (a good thing!) the question is, whether there is some exception thrown inside the using block (check with the debugger)
  • why are you using a transaction ? I can't see any reason which makes that necessary...

Please show some sample data with param value and expected result...

4 Comments

security and injection is not an issue for me here,it's local program for manipulating images.no ones gonna bother coming up with an attack.
select is working fine in sqlite manager,just not working in my program
no exception is thrown,I traced the program step by step,but still no clue.and somebody told me using transactions would be a good programing practice.
transactions are a good practice but not for everything... usually when you just SELECT there is seldomn need for a transaction... please show the valud of tag AND some sample data

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.