One of the questions I see most frequently on Freenode's ##csharp irc channel is how to use a MySql Database in .NET. I've therefore provided the class that I use for basic database operations. You can find it at http://www.martineve.com/2007/06/25/c-mysql-dataexecutor-class/.
The class supports both strongly and weakly typed datasets and usage is as follows:
- Get the MySql Connector.NET
- Reference it in your project
- Use the following for strongly typed datasets: </ul>
and this for weakly typed datasets:
Easy eh? :)
The parameterization of the queries will protect you against SQL Injection exploits so I'd recommend you do that at all times. Your connection string should go in your Web.Config file and then be specified inside the DataExecutor class. It's worth pointing out that if you don't set oldsyntax=true; then you must ensure that you use ? for your parameter names as opposed to @.