aristos
aristos

Reputation: 21

Finding a value in a MS Access table in c#

I am trying to help a friend on a project of his, however it requires some MS Access usage, which I am not familiar of. The task at hand is as follows:

We have a table, which consists of 3 rows. First row is in date format. Second and third are Yes/No columns. I am trying to return the value of second row of a given date. And I have written the code below to do this.

       string QueryDate= monthCalendar1.SelectionRange.Start.ToString();
       QueryDate= QueryDate.Substring(0, 10);

       OleDbConnection conn = new
       OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;DataSource=data.mdb");
       if (conn.State != ConnectionState.Open) conn.Open();
       string query = "Select Rezerve from 101 Where Tarih=\"QueryDate\"";
       OleDbDataAdapter adtr = new OleDbDataAdapter(query, conn);
       DataTable dt = new DataTable();
       adtr.Fill(dt);

However, it hardly accomplish the task at hand. I have found some code templates for displaying the corresponding values in visual interface. But, I can't find a way to return the value of the second row, 'Rezerve', to a variable to use it later. How can I do this? There are three cases by the way; first, there may be no record for the given date. Second,the second row may be 'yes', and third of course, it may be 'no'. I will have to call different functions for each of these cases.

Thanks in advance.

Upvotes: 0

Views: 1143

Answers (1)

dustinchilson
dustinchilson

Reputation: 136

This is just a rough idea of what you could do, I've done this before .

string QueryDate = monthCalendar1.SelectionRange.Start.ToString();
QueryDate = QueryDate.Substring(0, 10);

string connstr = "Provider=Microsoft.Jet.Oledb.4.0;DataSource=data.mdb";
string query = "Select Rezerve from 101 Where Tarih=@QueryDate";

using (OleDbConnection dc = new OleDbConnection(connstr))
{
    OleDbCommand sqlcmd = new OleDbCommand(query, conn);
    sqlcmd.Parameters.AddWithValue("@QueryDate", QueryDate);
    bool Rezerve = (bool)sqlcmd.ExecuteScalar();
}

switch (Rezerve) 
{ 
    Case true: 
        //true stuff 
        break; 
    Case false: 
        //false stuff 
        Break; 
    Default: 
        //No Return 
        Break; 
}

Upvotes: 1

Related Questions