How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Create a MySQL table dynamically in C#

Database

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.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

3 Responses to Create a MySQL table dynamically in C#

  1. feyzarc says:

    How to insert data into table that select by textbox.

  2. Jeff Pang says:

    How do you prevent SQL Injection if you don’t use parameters?

    • CooLMinE says:

      That depends on where you want to use the code above.

      If you are building a desktop application for a single user then you don’t care about SQL injections, although it is always recommended to sanitize the input, mainly in case you want to reuse your code for other projects that might actually be prone to an SQL injection.

      Other than that, you should always escape the input or using parameterized statements.

Leave a Reply

Your email address will not be published. Required fields are marked *