SELECT Query Example


	Command1 = new System.Data.SQLite.SQLiteCommand(Connection1);
	Command1.CommandText = "SELECT * FROM MyTable WHERE SomeColumnName >= @StartValue AND SomeColumnName <= @EndValue";
	Command1.Parameters.AddWithValue("@StartValue", 14);
	Command1.Parameters.AddWithValue("@EndValue", 28);

	System.Data.SQLite.SQLiteDataReader Reader1 = Command1.ExecuteReader();
	{
		while (Reader1.Read())
		{
			//MyVariable = Convert.ToString(Reader1["SomeColumnName1"]);
			//MyVariable2 = Convert.ToInt32(Reader1["SomeColumnName2"]);
		}
	}
	Reader1.Close();

Getting Count Of Rows Returned


	//Get the number of results that will be returned
	int RowCount = 0;
	Command1 = new System.Data.SQLite.SQLiteCommand(Connection1);
	Command1.CommandText = "SELECT count(*) FROM MyTable WHERE SomeColumnName >= @StartValue AND SomeColumnName <= @EndValue";		//The count() will return the number of results
	RowCount = Convert.ToInt32(Command1.ExecuteScalar());
	
	//Get the results
	Command1.CommandText = "SELECT * FROM MyTable WHERE SomeColumnName >= @StartValue AND SomeColumnName <= @EndValue";
	Command1.Parameters.AddWithValue("@StartValue", 14);
	Command1.Parameters.AddWithValue("@EndValue", 28);

	System.Data.SQLite.SQLiteDataReader Reader1 = Command1.ExecuteReader();
	{
		while (Reader1.Read())
		{
			//MyVariable = Convert.ToString(Reader1["SomeColumnName1"]);
			//MyVariable2 = Convert.ToInt32(Reader1["SomeColumnName2"]);
		}
	}
	Reader1.Close();

Reading DateTime In Select Statements

You need to specify you want the value returning in a DateTime format or you can just get the year portion returned!


	Command1.CommandText = "SELECT datetime(LogDateTime) as LogDateTime, SomeOtherColumnName FROM MyTable";

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.