300x250 AD TOP

Text Widget

Total Pageviews

9,499

Subscribe us

Contact Us

Name

Email *

Message *

Powered by Blogger.

You can also receive Free Email Updates:

Popular Posts

Followers

Thursday, 14 June 2012

Tagged under: , ,

Connecting C# to MySQL database / C# connection String for MySQL

In many cases it becomes imperative to connect C# to MySql, the prominent reason being the inherent simplicity of MySql. We have generally used the usual connection string to connect PHP to MySql. Connecting C# to MySql is similar.
We first need to use the connection string to establish the connection with MySql. Then, we use the SQL queries to carry out the Creation, Insertion, Update & Delete operations.


Before we do anything, first you need to download and install mysql-connector. You can get this connector from MySQL :: Download Connector/Net.

Once you install this connector, you need to add it as a reference in your C# project. You can do this by navigating to Project -> Add Reference. Then go to the .Net tab, and search for MySql.Data, and add it as a reference.

Now you need to include MySql.Data.MySqlClient at the start of the code. Add this at the start:


Now, you can use this connection string for establishing the connection:


Now, you can use 'con' when you need to fire any query.

Now, lets get into details of using sql queries.

1. CREATE TABLE:

                 
2. INSERT:

                 
3. UPDATE:

                 
4. DELETE:


5. SELECT:


Finally, you need to close the connection, after you finish executing the queries. You can do this by writing:
                 


0 comments:

Post a Comment