从三个表中获取数据

时间:2015-07-28 19:12:53

标签: c# database ms-access

我有这段代码:

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();

当我运行程序时,我收到一条消息说

  

“没有给出一个或多个必需参数的值”。

我想要的是,在AppT(约会表)中搜索是否有预定的任命(hasSchedule),然后检查是否匹配我提供的年份和月份(对当天不感兴趣),之后我转到AnimalsT以使用AnimalID获取AName(动物名称),然后我转到客户端以使用ClientID获取他们的信息FistName,LastName,Address,CellPhone。

1 个答案:

答案 0 :(得分:0)

以下代码行在您添加的参数的副本上设置值:

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

要解决此问题,请尝试使用此代码:

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;
相关问题