Hello all. I have the following code, with which I am attempting to select record in an Access database matching today's date, and for right now I'm simply writing that to the console. The problem I am having is that whenever I add the "where" clause, it breaks my program. If I take that one part out, and simply leave "select * from Mri2009", then it works like a champ.
I was writing the line "MyToday" just to make sure my date variable was being assigned correctly, which it is.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace MRILogToMRICT
{
class Program
{
static void Main(string[] args)
{
string MyToday = DateTime.Today.ToString("G");
OleDbConnection MyConnection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source = W:\inetpub\[URL unfurl="true"]wwwroot\ute_301_parsed\test.mdb");[/URL]
MyConnection.Open();
OleDbCommand MyCommand = new OleDbCommand("SELECT * FROM Mri2009 " + "WHERE DOS = ?", MyConnection); // + "WHERE DOS = 'MyToday'", MyConnection);
MyCommand.Parameters.AddWithValue("@DOS", OleDbType.Date.Equals(MyToday));
//also tried: just "DOS" without the @ - saw that on some web site so thought I'd give it a try.
try
{
OleDbDataReader thisReader = MyCommand.ExecuteReader();
while (thisReader.Read()) {
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}",
thisReader.GetValue(0),
thisReader.GetValue(1),
thisReader.GetValue(2),
thisReader.GetValue(3),
thisReader.GetValue(4),
thisReader.GetValue(5),
thisReader.GetValue(6),
thisReader.GetValue(7),
thisReader.GetValue(8),
thisReader.GetValue(9),
thisReader.GetValue(10));
}
}
catch (OleDbException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
MyConnection.Close();
Console.WriteLine("Connection Closed.");
Console.WriteLine(MyToday);
}
}
}
}
I was writing the line "MyToday" just to make sure my date variable was being assigned correctly, which it is.