Tag Archives: database

Using PowerShell to backup your MySQL databases

A few months ago we had to find a way to backup our MySQL databases, both remotely and locally. Instead of trying to find a software that did something similar I decided to write a few lines of code that solved our problem. Why spend time and money evaluating software when can create your own solution to the problem with little to no cost, right ?

The snippet below is fairly simple. The $databases variable holds an array of all the databases you want to dump. You will need to specify the database name, the path you wish to save the backup and the backup filename. I took the liberty of commenting the code in order to make it a bit more clear.

Please keep in mind that this solution uses mysqldump. This executable is usually located under C:\Program Files\MySQL\MySQL Server 5.6\bin, assuming you have MySQL server installed. In addition to mysqldump, since all our Windows servers we have WinRAR installed, this snippet uses WinRAR in order to compress the database files. You can always modify the code either skip the compression or to use .NET’s ZipFile class or 7zip.

This solution still needs some tweaking. For example, creating a temporary file with the passwords and passing it as an argument to mysqldump to avoid the “Specifying a password on the command line should be considered insecure” message, as well as some code cleanup and error catching. Until then, you are free to modify this example to better suit your needs.

If you have any questions feel free to post them in the comment section below.

Create a MySQL table dynamically in C#

Sometimes you might need to create database tables dynamically, based on new users, products etc. The snippet below allows you to do exactly that, create a new table in your database dynamically, straight from your code.

This example is heavily based on How to connect to a MySQL database and retrieve data in C# as the same concept of connecting to the database is required.

The first thing you need to do, as mentioned in the article linked above, is that you will need to add a reference to MySQL.Data in your project. After you have done that, you can modify the snippet below to create the table with the columns you want based on your requirements.

If you are unsure as to which database engine to use, please take a look at https://dev.mysql.com/doc/refman/5.0/en/storage-engines.html as every database engine has different type of limitations, so it is better to choose the one that better suits your needs.

How to connect to a MySQL database and retrieve data in C#

Connecting to a MySQL database is fairly simple and straight forward. The first thing you will need to do is to add a reference to MySQL.Data in your project.

This can be done in a few ways:

  • Right click on references in your project -> click Manage NuGet packages -> Search and install MySQL.Data from Oracle
  • Open Package Manager Console from Tools -> NuGet Package Manager -> Paste in the console the following line: Install-Package MySql.Data
  • If you have installed the .NET Connector from MySQL you can navigate to where you have installed the connector and add a reference to the MySQL.Data.dll file

After you have added a reference to MySQL.Data you can tailor the snippet below to suit your requirements.

If you are interested in other type of databases you can check my Microsoft Access Database and SQLite database tutorials.

Top 5 Most Popular Databases 2014

While parsing the data for Top 10 Most Popular Programming Languages 2014 I decided to make another chart that would illustrate which are the top 5 most popular databases in 2014.

As I have mentioned in the post above as well, the chart below is nowhere near 100% accurate as there are a lot of variations for each tag.

For my findings I used Stack Overflow’s database to query the posts that were created in 2014, then split and shorted the tags based on how many times they appeared in the posts. After parsing and processing 9307 unique tags with a total of 97386 occurrences, these are the results.

Top 5 Databases 2014

Raw Data

Tag NameTag Count
MySQL54894
SQL-Server25164
MongoDB7605
SQLite5090
PostgreSQL4633

Creating and connecting to a Microsoft Access Database programmatically in C#

The following example aims to get you a bit more familiar as to how to create and connect to a Microsoft Access database programmatically while being able to add any type of tables you want as well are inserting and retrieving data from it.

Firstly we will need to reference two libraries in our project. Microsoft ActiveX Data Objects 2.0 Library (Interop.ADODB.dll) which we will use to ensure that the connection to the database is closed after we are done creating it, and Microsoft ADO Ext. 2.8 for DDL and Security (Interop.ADOX.dll) which is needed to be able to access the classes that are required to create our database.

After you have finished adding those two references to your project you can use the code sample below to create the database file programmatically. Feel free to modify the code to suit your needs. Also please note that setting your project to Any CPU build will cause exceptions to be thrown when creating the database.