简体   繁体   中英

Get data from three tables

I have this code:

connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select AName, FirstName, LastName, Address, CellPhone from ClientsT, AnimalsT where AppT.hasSchedule = @T and Month(AppT.ScheduleApp)= @MonthVacine and YEAR(AppT.ScheduleApp)= @YearVacine and AnimalT.AnimalID = AppT.AnimalID and AnimalsT.ClientID = ClientsT.ClientID";
command.CommandText = query;
command.Parameters.Add("@T", OleDbType.Boolean).Value = true;
command.Parameters.Add("@MonthVacine", OleDbType.Char).Value = month;
command.Parameters.Add("@YearVacine", OleDbType.Char).Value = year;
OleDbDataReader reader = command.ExecuteReader();
listView2.Items.Clear();

while (reader.Read())
{
    ListViewItem item = new ListViewItem(reader["AName"].ToString());
    item.SubItems.Add(reader["FirstName"].ToString());
    item.SubItems.Add(reader["LastName"].ToString());
    item.SubItems.Add(reader["Address"].ToString());
    item.SubItems.Add(reader["CellPhone"].ToString());

    listView2.Items.Add(item);
}
reader.Close();

connection.Close();

When I run the program I get a message saying

"no value given for one or more required parameters".

What I want, is away of searching in the AppT (appointments table) if there is an appointement scheduled (hasSchedule) and then check if matches the year and month that I provide (not interested in the day), after this I go to AnimalsT to get the AName (animal name) using AnimalID, and then I go to Clients to get their information FistName, LastName, Address, CellPhone using ClientID.

The following code lines are setting the Value on a copy of the parameter you added:

command.Parameters.Add("@T", OleDbType.Boolean).Value = true;
command.Parameters.Add("@MonthVacine", OleDbType.Char).Value = month;
command.Parameters.Add("@YearVacine", OleDbType.Char).Value = year;

To fix this try this code instead:

command.Parameters.Add("@T", OleDbType.Boolean);
command.Parameters.Add("@MonthVacine", OleDbType.Char);
command.Parameters.Add("@YearVacine", OleDbType.Char);

command.Parameters["@T"].Value = true;
command.Parameters["@MonthVacine"].Value = month;
command.Parameters["@YearVacine"].Value = year;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM