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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | string serverIp = "<Server IP or Hostname>"; string username = "<DB username>"; string password = "<DB password>"; string databaseName = "<DB name>"; string dbConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};", serverIp, username, password, databaseName); string query = "SELECT * FROM YourTable"; var conn = new MySql.Data.MySqlClient.MySqlConnection(dbConnectionString); conn.Open(); var cmd = new MySql.Data.MySqlClient.MySqlCommand(query, conn); var reader = cmd.ExecuteReader(); while (reader.Read()) { var someValue = reader["SomeColumnName"]; // Do something with someValue } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect to www" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^www\.<your domain>\.com$" negate="true" /> </conditions> <action type="Redirect" url="https://www.<your domain>.com/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
Posted in Software Tips.
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.
Posted in Software News.
Tagged Android, iOS, Microsoft, Visual Studio, Visual Studio 2012, Visual Studio 2013, Visual Studio 2015, Visual Studio Community, Xamarin
Google’s Nik Collection photo editing software is now free
Nik Collection used to cost $149 until a few days ago. Google has announced that the bundle will now be available for free. If you already own Google’s Nik Collection and have made the purchase during the year 2016 you are in luck as Google announced that you will receive a full refund.
Google’s Nik Collection consists of seven desktop plug-ins, all of them which support both Photoshop and Lightroom. Nik Collection provides a powerful range of photo editing capabilities — from filter applications that improve color correction, to retouching and creative effects, to image sharpening that brings out all the hidden details, to the ability to make adjustments to the color and tonality of images.
- Analog Efex Pro – Explore the look and feel of classic cameras, films, and lenses.
- Color Efex Pro – A comprehensive set of filters for color correction, retouching, and creative effects.
- Silver Efex Pro – Master the art of black-and-white photography with darkroom-inspired controls.
- Viveza – Selectively adjust the color and tonality of your images without complicated masks or selections.
- HDR Efex Pro – From natural to artistic, explore the full potential of HDR photography.
- Sharpener Pro – Bring out hidden details consistently with the professional’s choice for image sharpening.
- Dfine – Improve your images with noise reduction tailored to your camera.
More information and download links regarding Google’s Nik Collection can be found in Google’s Nik Collection website.
Hardware Requirements
Windows
- Windows Vista®, Windows 7, Windows 8
- Adobe Photoshop CS4 through CC 2015
- Adobe Photoshop Elements 9 through 13 (apart from HDR Efex Pro 2, which is not compatible with Photoshop Elements)
- Adobe Photoshop Lightroom 3 through 6/CC
Mac
- Mac® OS X 10.7.5 through 10.10
- Adobe Photoshop CS4 (CS5 for HDR Efex Pro 2) through CC 2015
- Adobe Photoshop Elements 9 through 13 (apart from HDR Efex Pro 2, which is not compatible with Photoshop Elements)
- Adobe Photoshop Lightroom 3 through 6/CC
- Apple® Aperture® 3.1 or later
Force HTTPS using IIS
Google announced in their blog that it will start favoring websites that use HTTPS. Google ranking and increased security for your users is two major reasons why you should consider getting an SSL certificate and moving your website to HTTPS.
If you are using IIS you can use the below snippet in your web.config in order to redirect all HTTP requests to HTTPS.
For your convenience, in case you are not familiar how to use the snippet below, I have attached an image to illustrate how the rewrite rule will look using IIS’s interface, so if you are having trouble with the web.config, simply mirror the details in the image.
Please keep in mind that to use the snippet below you will need to have URL Rewrite module installed and enabled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
Posted in Software Tips.