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)
You don’t need the transaction, try the following:
The most likely reason this won’t return anything is if the
SELECTdoesn’t yield any results.Also note that anything implementing the
IDisposableinterface can be used in conjunction with theusingstatement, 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.