Working with SqlDataSource: MySQL and LIKE clause

I'm working in a small project which is designed with a MySQL database. As you and I know, MySQL fits perfectly this type of project. I'm not saying that it doesnt fit big projects, but MySQL is great for small projects ;)

We could have used SQL Express too, but hey, it is cool to be versatile ;)

Since it is a small project, I though we could use ASP.NET controls to display data. I've been working for years with ASP.NET but never worked with the SqlDataSource, for example, just with the ObjectDataSource which were bounded to Business Objects. But I knew SqlDataSource capabilities, since I studied it for certification exams :)

One mistake I found very common while googling is to think that the SqlDataSource objects doesnt work with other databases than Microsoft SQL Server. SqlDataSource works with every database that has a ADO.NET provider implemented, including MySQL. MySQL has the marvelous MySQL Net/Connector, which is at version 6 and can be downloaded here.

MySQL Net/Connector has alot of "gotchas", some things works different with the MySQL database as a data source for the SqlDataSource, specially the SELECT querys with "LIKE" clauses, and that's what this post is all about.

First things first, let's make the MySQL Net/Connector work. Close all yours Visual Studio instances, then you install the MySQL Net/Connector, which is next, next, next, finish.

Fires up your Visual Studio again and you can add a SqlDataSource to a Web Form. Right click the SqlDataSource object and click "Configure Data Source", a Wizard is going to start. Click "New connection" in the first screen and you are going to see an option to connect to a MySQL database, like this:

Choose "MySQL Database", click "Ok" and fulfill the connection information to your database:

Click "Next", choose to save connection string in the web.config file (we all know this isn't safe, but this is an example) and click "Next" again.

The query statement wizard starts, and this is where things starts to get interesting, specially if you need to add a LIKE clause to your query:

Click the "WHERE" button and add a LIKE clause, like in the screen below:

Click "Add" and then "OK".

Back to the query statement wizard, click "Next" and watch as the wizard mounted the query in the "SELECT statement" box:

If you click the "Test Query" button and add a value to the parameter you added two steps ago, a error is going to occur:

After hours bumping my head against the wall, I found out that the query statement that Visual Studio generated isn't MySQL compliant:

The query's syntax demonstrated above works with SQL Server, but not with MySQL, which should be:
SELECT * FROM conteudo WHERE Titulo LIKE Concat('%', ?Texto, '%')

The "Test Query" will still not work and I don't know why, but it works at runtime.

I appreciate your comments ;)

Updates:

  • I found out that the images I posted were too large, so I decreased its size.
  • I don't even know how my post was submited to DotNetShoutOut. Whoever made it, thank you. From now on I'm going to post the "Shout it" in every post! If you like the content, please shout it!

Shout it

13 Comments

  • i did it selecting the option

    especify a custom SQL..........

    and it works percfectly
    .

  • Emerson. Are you using MySQL too?
    I found a lot of people with the same problem as me while searching a solution.

  • Great sample, just the thing I needed.

    If there are more samples like this, please share with us!

    I started with SqlDataSource but later on I'll need also GridView and other stuff

  • I try to connect MySQL via sqlDataSource. I use VWD 2008 Express, MySQL 5.0.87-community-nt and MySQL Connector/Net 6.2.2. In my web project, I not find MySQL Database in Data Source. Why! Help me!

  • I have install .net connector, but why i cant fill in the localhost and it disappear when i type word?

  • I just ran into a similar problem and found that I simply had to remove the angle brackets around the table definition in the from clause.

    Wrong for MySQL : select * from [contuedo] ...
    Wright for MySQL: select * from contuedo ...

    Some options are missing however.

  • help cod for like con aspnet and Mysql

  • I have the same problem with mysql...

  • I know that :

    Wrong for MySQL : select * from [contuedo] ...

    Wright for MySQL: select * from contuedo ...

    But I don't know that how can we use LIKE operator with it

  • solved:
    Only use ?


    SELECT * FROM conteudo WHERE Titulo
    LIKE Concat('%', ?, '%'))

  • Dude, you saved my life! Cheers for this post

  • Thanks very much, you saved my life too, great post!!

  • Using a Like search, I am getting different records returned depending on whether I use an ORDER BY Clause at the end of the select statement and I'm still not getting all the records returned. This must surely be a bug and I can't find any way around. Any help appreciated. I've spent days on this.
    WHERE (RestaurantsDetails.Description Like Concat('%', ?, '%')ORDER BY Restaurants.Town ASC; "
    returns some records the same and some different from
    WHERE (RestaurantsDetails.Description Like Concat('%', ?, '%'); "

Comments have been disabled for this content.