[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

ODBC.Net Connections

I'm going mad just trying to get the correct result from an Access database through ODBC using C#. I've got the following:

private void dishNameBox_TextChanged(object sender, System.EventArgs e)
            {
                  textBox1.Text = "";
                  string sql = "SELECT d_name FROM dishes WHERE d_name LIKE '" + dishNameBox.Text.Trim() + "*'";

                  OdbcCommand command = new OdbcCommand(sql, conn);
                  conn.Open();

                  OdbcDataReader reader = command.ExecuteReader();
                  while (reader.Read())
                  {
                        textBox1.Text += reader.GetString(0);
                  }
                        
                  reader.Close();
                  conn.Close();
            }

where conn is a tested OdbcConnection. The problem is that the reader OdbcDataReader doesn't want to return anything even when the query should (I've tested it directly in Access). In fact, when I remove the 'WHERE' part of the SQL, it returns all of the d_names as required.

Any ideas?
0
_WolfyUK
Asked:
_WolfyUK
1 Solution
 
purpleblobCommented:
I notice you are using an asterix * for your wildcard character. I realize this works in Access, but the usual wildcard is % which is used for SQL in general.

Just to check this I set up an OdbcDataAdapter and went through the process of setting up a LIKE query using % and this works, whereas * doesn't appear to work in my tests.

Hope this helps
0
 
_WolfyUKAuthor Commented:
I first used a % when I was having other problems and copied the SQL to Access... and it failed so I replaced it with a *. But ODBC seems to like converting things like that when it wants to, so switching this back to a % worked fine. Cheers!

Why does my customer have to use this dodgy software?  :(
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now