We are testing interview quizzes and created a demo app. Now, We require your help. Please take this quiz and provide inputs for content improvement. Interview Quiz




How to make a Connection between c# and mysql?

databaseDatabase @Image courtesy of ddpavumba/ FreeDigitalPhotos.net


This is my first article on codespread.com, titled ‘Connection between c# and mysql’.Before going into details, we should know that mysql is a open source software for data base management system. We use Microsoft visual c# and sql data source quite often , But in this tutorial I will show how we can connect mysql database and c# windows application.

Screen 1 : We have created a demo application. Inside it,we place a Button.

DemoApplication

Where on the button click , we can create a table to the specified mysql database.

Screen2 : We have created a table in mysql database.

MySqlDatabase

Next, In c# class file, import these namespaces.


using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using MySql.Data.MySqlClient ;

Where the above namespace “MySql.Data.MySqlClient” is the dll file ,in which all the connection commands will be residing .This can be found in mysql connector to c#/.net.Once you have imported the dll, you need to add this reference to your current project.


namespace DemoApplication

{

/// <summary>

/// Description of MainForm.

/// </summary>

public partial class MainForm : Form

{

public MainForm()

{

// // The InitializeComponent() call is required for Windows Forms designer support. //

InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. //

}

/* here What I have done is on button click, I have inserted a set of commands that creates a table in mysql database */

public void Button1Click(object sender, EventArgs e)

{

string quer= "SERVER=localhost; " + "DATABASE=sathish;" + "USERID=root;" + "PASSWORD=root;" ;

/*The above command is used to connecting the mysql with c# , Where you need to select appropriate database ,userid and password */

MySqlConnection con = new MySqlConnection(quer); con.Open() ;

/* You can use mysqlcommand or command builder for running the querries */

MySqlCommand cmd=new MySqlCommand();

try

{
/*Specifcation of the mysql connection over here */

cmd.Connection=con ;

/*Command text is where we need to insert our queries */

cmd.CommandText="create table Demo(userid varchar(20),Password varchar(20));" ;

/*Lastly, we need to execute the queries using ExecuteNonQuery of Mysqlcommand */

cmd.ExecuteNonQuery();

}

catch(MySqlException ex)

{

label1.Text = ex.Message;

}

}

}

}

Note: I prefer to build Mysqlconnection with an exception block so it would be easier diagnosing the error.
You can download mysql connector and mysql to your system from www.Mysql.com . Next I’ll come with how you can insert,update and select operations using DataGrid.I hope, I was able to help you.For any queries, please comment over here or send your queries or articles to admin@codespread.com

Navigation:





2 Responses to How to make a Connection between c# and mysql?

  1. Good first post! But since “MySql.Data.MySqlClient.dll” isn’t part of .NET Framework, perhaps you could have pointed your readers to where they could get it downloaded and installed on their systems. Anyway, it’s a pretty good article.