Monthly Archives: April 2016

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.

Add www in-front of your domain using IIS

If you are using IIS you can use the snippet below in your web.config to add www. in-front of your domain if it is not already present.

Please keep in mind that to use the snippet below you will need to have URL Rewrite module installed and enabled.

Xamarin is now included for free in Visual Studio

Starting April of 2016, Visual Studio now includes Xamarin for free. This is the case for every edition of Visual Studio, including Visual Studio Community Edition, which is free as well.

With Xamarin you can develop and publish native apps for iOS and Android using C# or F# from directly within Visual Studio with no limits on app size.

For developers that are using Mac, Xamarin Studio is now available as a benefit of your Visual Studio Professional or Enterprise subscription. Developers can use the newly-created Xamarin Studio Community Edition for free.

You can visit Xamarin’s store to download Xamarin for free. Keep in mind that, Xamarin, although free, is license restricted for bigger companies. If you want to use Xamarin you will need to comply to one of the following license terms:

  • Individual license – If you are an individual working on your own applications to sell or for any other purpose, you may use the software to develop and test those applications.
  • Organization licenses – If you are an organization, your users may use the software as follows:
    • Any number of your users may use the software to develop and test your applications released under Open Source Initiative (OSI) approved open source software licenses.
    • Any number of your users may use the software to develop and test extensions to Visual Studio.
    • Any number of your users may use the software to develop and test your applications as part of online or in person classroom training and education, or for performing academic research.
    • If none of the above apply, and you are also not an enterprise (defined below), then up to 5 of your individual users can use the software concurrently to develop and test your applications.
    • If you are an enterprise, your employees and contractors may not use the software to develop or test your applications, except for open source and education purposes as permitted above. An enterprise is any organization and its affiliates who collectively have either (a) more than 250 PCs or users or (b) more than one million US dollars (or the equivalent in other currencies) in annual revenues, and enterprise means those entities that control (via majority ownership), are controlled by, or are under common control with an organization.
  • Demo use – The uses permitted above include use of the software in demonstrating your applications.