tags:

views:

294

answers:

5

I'm using Microsoft Visual C# 2008 Express Edition with SqlLite. I'm successfully able to open my database and with C# code, add entries to my tables.

When it comes to retriving data, I'm having some issues and have been searching and searching the internet for basic tutorial information on how to do these basic things...

Here's my code... (after I've opened up a connection to the database which is called 'conn' here):

SQLiteCommand cmd = new SQLiteCommand(conn);
cmd.CommandText = "select myField1,myField2 from myTable where myField3 = '" + tempstring + "';";
cmd.CommandType = CommandType.Text;
SQLiteDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
  string tmp = reader.GetString(0);
  System.Console.WriteLine(" my output = " + tmp);
}

When I execute this... I get no errors and because I get no output on that last line, it looks like the while loop is not executing at all.

I'm a beginner to this stuff... what am I missing and is there a good web resource where I can learn these basic things? I'm pretty comfortable in SQL on it's own... just not integrated in C# like this...

Thanks!

-Adeena

+5  A: 

First, remove the hurtful trailing semicolon from the line while (reader.Read());...!

Alex Martelli
@Alex: Excellent eye!
bbmud
sorry.. that was a typo here on the post... I typed instead of copy-pasted
adeena
+2  A: 

This looks correct to me. Does the property reader.HasRows return true for your query?

A couple of side issues to be aware of are:

  1. Be sure to dispose of your SQL resources by wrapping your objects in using { } blocks.
  2. Consider using parameterized queries instead of injecting the query parameter directly in the SELECT statement.
Kevin Pullin
can you explain what #2 means here in C#?
adeena
hasrows = false... okay... I'm starting to see part of my issue. :)
adeena
ok - I replaced "tempstring" with an actual value in the query... and it's working... so I think my issue is related to the fact that tempstring as a parameter isn't working the way I think it should
adeena
Check this link for some details on parameterized queries with SQLLite: http://sqlite.phxsoftware.com/forums/t/83.aspx (from the author of the .Net SQLite wrapper).
Kevin Pullin
thanks for the link!
adeena
A: 

Good one, Alex.

In addition to that and since you are beginning with sqlite (you may want to delete second L from the tag), remember that sqlite does not really guaranty data type safety on the database level.

van
+1  A: 

Answering your question on how to write parameterized queries:

cmd.CommandText = "select myField1,myField2 from myTable where myField3 = @tempString;";
SQLiteParameter param = new SQLiteParameter("@tempString");
cmd.Parameters.Add(param);

// you can modify that value without touching the sql statement (which means you could cache the above command)
param.Value = tempstring;
SQLiteDataReader reader = cmd.ExecuteReader();
[...]

Parameters in SQLite can have several forms which you can find here.

See here for more info on parameterized queries.

VVS
thanks for the info and links! This helps and will probably be extremely useful since I will have a lot of this going on in my code once I get further along...
adeena
A: 

Not to divert you from your Sqlite question, but if you are having comfort issues with Sqlite queries embedded in C#, you could try NHibernate coupled with Fluent NHibernate. These technologies provide an excellent data access mechanism into databases, including Sqlite.

NHibernate requests into Sqlite are very fast, and you won't have to worry about some of the Sqlite idiosyncrasies. If you build out your data-access layer properly with NHibernate, you should be able to up-scale to a more robust database very quickly.

Jeff Fritz
I wouldn't say it's a comfort thing... just a whole new thing for me to learn and I have yet to find a good resource. I found some great stuff on just getting up and running with SQLite, but haven't found good examples of use in C#. :)
adeena