SolidFire
SolidFire

Reputation: 11

Searching a access database for a part of a word

I have an access database that contains columns such as Title, Director, And Year.

I have access database connected to the program. I am able to search the database and print out the title of the movie to a listBox only if the title contains the one word. For example, if I search for the movie "psycho" the movie will display in the listBox.

Now I'm wondering how I can search for a particular word like "the" and print all the movie titles with "the" in it to the listBox.

At that point, the text book I'm using fails to go into anything deeper. Maybe I'm not using the right method for what I'm trying to do.

private void searchButton_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        string lowerCaseData = textBox1.Text.ToLower();
        if (titleButton.Checked)
        {
            var query =
                from m in this.moviesDataSet.Movies
                where m.Title == lowerCaseData
                select m;
            foreach (var m in query)
            {
                listBox1.Items.Add(m.Title);
            }

        }
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'moviesDataSet.Movies' table. You can move, or remove it, as needed.
        this.moviesTableAdapter.Fill(this.moviesDataSet.Movies);

    }

Upvotes: 0

Views: 530

Answers (2)

Habib
Habib

Reputation: 223312

Instead of exact match check for Contains


Depending on the LINQ provider, Contains check should be translated to SQL LIKE statement which will be case insensitive, so your check should be:

where m.Title.Contains(lowerCaseData)

However for in-memory collections, Contains would perform a case sensitive match. In that case String.IndexOf can be used like:

where m.Title.IndexOf(lowerCaseData, StringComparison.OrdinalIgnoreCase) >= 0)

Upvotes: 1

Steve Mitcham
Steve Mitcham

Reputation: 5313

Your where query is doing an exact match. Use Contains instead

where m.Title.ToLower().Contains(lowerCaseData)

Upvotes: 0

Related Questions