解析查询时出错

时间:2012-06-02 12:24:14

标签: c# sql visual-studio-2010 sql-server-ce

我正在使用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,令牌   line offset = 18,令牌错误=。 ]

任何帮助?

提前感谢!

1 个答案:

答案 0 :(得分:4)

我怀疑这是问题所在:

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

我怀疑你的意思是:

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

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