简体   繁体   中英

How to use Like operator in c# using ms access database?

I'm making a Windows Form Application for our project my project is about scheduling system, and I'm trying to search data from my ms access database that contains Day and Instructor. The Day column has only two values "MWF and TTH". And when i search MWF i want all the data that contains MWF to display to my listview but only one data will display. How can I fix this ?

CODE HERE

public void Search()
    {
        c.OpenDb();
        c.com.CommandType = CommandType.Text;
        c.com.CommandText = "SELECT * FROM ScheduleInfo WHERE Day LIKE '%" + comboBox1.Text + "%' AND Instructor LIKE '%" + textBox9.Text + "%' ";
        c.com.Connection = c.con;
        c.dr = c.com.ExecuteReader();
        if (c.dr.Read())
        {
            ListViewItem fp = new ListViewItem(c.dr["Day"].ToString());
            fp.SubItems.Add(c.dr["Time"].ToString());
            fp.SubItems.Add(c.dr["Instructor"].ToString());
            fp.SubItems.Add(c.dr["Subject"].ToString());
            fp.SubItems.Add(c.dr["Room"].ToString());
            listView5.Items.Add(fp);

        }
        else
        {
            Notfound nf = new Notfound();
            nf.ShowDialog();
            if (nf.DialogResult == DialogResult.OK)
            {
                textBox9.Clear();
            }
        }
        c.CloseDb();
    }

    private void button13_Click(object sender, EventArgs e)
    {
        Search();
    }

Change if (c.dr.Read()) into while (c.dr.Read()) :

    bool hasRecords = false;

    while (c.dr.Read()) {
      ListViewItem fp = new ListViewItem(c.dr["Day"].ToString());

      fp.SubItems.Add(c.dr["Time"].ToString());
      fp.SubItems.Add(c.dr["Instructor"].ToString());
      fp.SubItems.Add(c.dr["Subject"].ToString());
      fp.SubItems.Add(c.dr["Room"].ToString());
      listView5.Items.Add(fp);

      hasRecords = true;
   }

   if (!hasRecords) {
     Notfound nf = new Notfound();

     nf.ShowDialog();

     if (nf.DialogResult == DialogResult.OK)
        textBox9.Clear();
   }

Access uses the * character as a wildcard, not the % character.

http://www.techrepublic.com/article/10-tips-for-using-wildcard-characters-in-microsoft-access-criteria-expressions/

SQL Server uses the % character as a wildcard. Maybe that's what you were thinking of.

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