MySQL is an opensource database avilable at free of cost, So the development with MySQL database will be increasing day by day. Most of the developers are working with Microsoft SQL server only, But lets have an idea about the connection with MySQL server. Here is a small example about MySQL Connection with C# winforms application.
Before starting the work you are requested to install a MySQL server in your machine. Create database and necessary tables you want.
Go to MySQL website and download the MYSQL connector to use with our .NET Application.
Here is the link for same
http://dev.mysql.com/downloads/connector/net/5.0.html
Once you downloaded, then try to install it.
After a successfull installtion of MySQl and MySQL Connector you can move to your project.
Go to Visual Studio >> Solution Explorer >> Add references and add a reference to MySQL.DATA from .net components.
Then Import the following namespaces to your program
using MySql.Data.MySqlClient; using MySql.Data.Types;
Thats all others are same as our normal SQL Connection and OLEDB Connection
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; using MySql.Data.Types;
namespace WindowsFormsMySqlConnectEg { public partial class MathewsMySQLConnectionEg : Form { private string strProvider = "Data Source=localhost;Database=mathewsdb;User ID=root;Password=root";
public MathewsMySQLConnectionEg() { InitializeComponent(); }
private void btnAdd_Click(object sender, EventArgs e) { Insert(); }
public void Insert() { string insertSQL = "INSERT INTO employee (name, address) VALUES ('" + textBox1.Text.ToString() + "', '" + textBox2.Text.ToString() + "')"; MySqlConnection objMyCon = new MySqlConnection(strProvider); MySqlCommand cmd = new MySqlCommand(insertSQL, objMyCon); objMyCon.Open(); int executed = 0; executed = cmd.ExecuteNonQuery(); objMyCon.Close();
if (executed == 1) { MessageBox.Show("Added","MySQL Example Says.. "); textBox1.Text = ""; textBox2.Text = ""; FillGrid(); } else { MessageBox.Show("Unable to add", "MySQL Example Says.. "); } }
public void FillGrid() { string sql = "SELECT ID, NAME, ADDRESS FROM EMPLOYEE ORDER BY NAME"; MySqlConnection objMySqlCon = new MySqlConnection(strProvider); MySqlCommand cmdSel = new MySqlCommand(sql, objMySqlCon); DataTable dt = new DataTable(); MySqlDataAdapter da = new MySqlDataAdapter(cmdSel); da.Fill(dt); dataGrid1.DataSource = dt; }
private void MathewsMySQLConnectionEg_Load(object sender, EventArgs e) { FillGrid(); }
} }
The Above Example will fetch a Name and Address from textboxes and stores in database, it also shows the database table values in a Datagrid.
Database Tables used here
Table Name : employee
Fields - Datatype id Integer Auto Increment name varchar(200) address varchar(400)
This will be a most simple and accurate example regarding mysql connection with Asp.net or C#
|
| Author: Rahul Ramesh 24 Jun 2009 | Member Level: Bronze Points : 2 |
Hi, I have found this one as the simplest tutorial to connect asp.net and mysql. I havent tried it yet, abt to try. But befor that, i have a question. Iam developing my project in my pc, will i have to any adjustments to the server when iam publishing my asp.net (C#) site? Or will the app work as such without any modification to the server? Iam a graduation student and iam new to asp.net, i have used PHP and MySQL for my web development (well, just small stuffs). Thats why iam interested in ASP.Net and MySQL. Will keep posting, after i test your method. Reagards, Rahul Ramesh.
|
| Author: Rahul Ramesh 24 Jun 2009 | Member Level: Bronze Points : 1 |
Wow sir, your lines made asp.net MySql connection a cakewalk for me. I have tested the method you said, it worked just like that. Thank you so much. :-)
|