解析查询时出错

本文关键字:出错 查询 | 更新日期: 2023-09-27 18:36:15

我正在使用Visual Studio 2010用C#编写程序,并在从.sdf文件中检索数据时出现错误。

private void BasicSearch_button_Click(object sender, EventArgs e)
{
    SqlCeConnection cn = new SqlCeConnection(@"Data Source=" + Path.Combine(Application.StartupPath, "FRAT_DB.sdf") + ";Password=P@ssw0rd;Persist Security Info=False;Max Database Size=256");
    string query = @"SELECT Person.PersonID, Person.Name, PhotoTags.PhotoID, Photo.Path, Photo.Location, Person.Age, Person.P_Group, Person.Email, Photo.Date "+
                    "FROM Person INNER JOIN PhotoTags ON Person.PersonID = PhotoTags.PersonID INNER JOIN "+
                    "PhotoON PhotoTags.PhotoID = Photo.PhotoID "+"WHERE (Person.Name LIKE '%%')";
    if (txtName1.Text.Trim().ToString() == "" || txtName2.Text.Trim().ToString() == "")
    { MessageBox.Show("Enter Both, Name1 and Name2"); return; }
    else if (radioButtonAND.Checked)
    {
        query = @"select pt.PhotoID,Photo.Path,photo.Location, Photo.Date from Person p inner join PhotoTags pt on p.PersonID=pt.PersonID inner join Photo on pt.photoID=photo.photoID where Name like '%" + txtName1.Text + "%' and pt.Photoid in (select pt.PhotoID from Person p inner join PhotoTags pt on p.PersonID=pt.PersonID where Name like '%" + txtName2.Text + "%')";
    }
    else
    {
        query = @"SELECT DISTINCT Person.PersonID, Person.Name, PhotoTags.PhotoID, Photo.Path, Photo.Location, Person.Age, Photo.Date
FROM Person INNER JOIN PhotoTags ON Person.PersonID = PhotoTags.PersonID INNER JOIN
PhotoON PhotoTags.PhotoID = Photo.PhotoID
WHERE (Person.Name LIKE '%%')";
        query += " AND (Person.Name like '%" + txtName1.Text + "%' OR Person.Name like '%" + txtName2.Text + "%')";
     }
     if (cn.State == ConnectionState.Closed) cn.Open();
     SqlCeCommand cm_Search = new SqlCeCommand(query, cn);
     try
     {
         SqlCeDataReader rdr = cm_Search.ExecuteReader();
         List<PersonPhoto> personPhoto = new List<PersonPhoto>();
         List<PersonPhotoWithoutName> personPhotoWithoutName = new List<PersonPhotoWithoutName>();
         bool HasRows = rdr.Read();
         if (HasRows)
         {
            while (rdr.Read())
            {
                if (radioButtonAND.Checked)
                {
                    personPhotoWithoutName.Add(new PersonPhotoWithoutName
                            {
                                PhotoID = Convert.ToInt32(rdr["PhotoID"].ToString()),
                                Location = rdr["location"].ToString(),
                                Date = rdr["Date"] != null ? Convert.ToDateTime(rdr["Date"]) : DateTime.Now,
                                path = rdr["path"].ToString(),
                            });
                }
                else
                {
                    personPhoto.Add(new PersonPhoto
                            {
                                Name = rdr["Name"].ToString(),
                                Location = rdr["location"].ToString(),
                                Date = rdr["Date"] != null ? Convert.ToDateTime(rdr["Date"]) : DateTime.Now,
                                path = rdr["path"].ToString()
                            });
                }
             }
             rdr.Close();
          }
          else
          { MessageBox.Show("No Records Found"); selectedPictureBox.Image = null; rdr.Close(); }
          if (personPhoto.Count > personPhotoWithoutName.Count)
          {
              DataGridPersons.DataSource = personPhoto;
              DataGridPersons.Refresh();
          }
          else
          {
              DataGridPersons.DataSource = personPhotoWithoutName;
              DataGridPersons.Refresh();
          }
      }
      catch (Exception exp)
      {
                throw exp;
      }
      if (cn.State != ConnectionState.Closed) cn.Close();
 }

每当我尝试搜索某人时,都会出现此异常错误:

解析查询时出错。[令牌行号=3,令牌 行偏移量 = 18,标记错误 = 。]

有什么帮助吗?

提前感谢!

解析查询时出错

我怀疑这是问题所在:

... INNER JOIN
PhotoON PhotoTags.PhotoID = Photo.PhotoID ...

我怀疑你的意思是:

... INNER JOIN
Photo ON PhotoTags.PhotoID = Photo.PhotoID ...

请注意 PhotoON 之间的空格。老实说,我怀疑您可以重新格式化查询,这样会更清楚......